<commit_before>// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.athenz;

import com.yahoo.vespa.hosted.controller.api.identifiers.AthenzDomain;
import com.yahoo.vespa.hosted.controller.api.identifiers.UserId;

import javax.naming.NamingException;
import javax.naming.ldap.LdapName;
import java.security.cert.X509Certificate;

/**
 * @author bjorncs
 */
public class AthenzUtils {

    private AthenzUtils() {}

    public static final AthenzDomain USER_PRINCIPAL_DOMAIN = new AthenzDomain("user");
    public static final AthenzDomain SCREWDRIVER_DOMAIN = new AthenzDomain("cd.screwdriver.project");
    public static final AthenzService ZMS_ATHENZ_SERVICE = new AthenzService("sys.auth", "zms");

    public static AthenzIdentity createAthenzIdentity(AthenzDomain domain, String identityName) {
        if (domain.equals(USER_PRINCIPAL_DOMAIN)) {
            return AthenzUser.fromUserId(new UserId(identityName));
        } else {
            return new AthenzService(domain, identityName);
        }
    }

    public static AthenzIdentity createAthenzIdentity(String fullName) {
        int domainIdentityNameSeparatorIndex = fullName.lastIndexOf('.');
        if (domainIdentityNameSeparatorIndex == -1
                || domainIdentityNameSeparatorIndex == 0
                || domainIdentityNameSeparatorIndex == fullName.length() - 1) {
            throw new IllegalArgumentException("Invalid Athenz identity: " + fullName);
        }
        AthenzDomain domain = new AthenzDomain(fullName.substring(0, domainIdentityNameSeparatorIndex));
        String identityName = fullName.substring(domainIdentityNameSeparatorIndex + 1, fullName.length());
        return createAthenzIdentity(domain, identityName);
    }

    public static AthenzIdentity createAthenzIdentity(X509Certificate certificate) {
        return createAthenzIdentity(getCommonName(certificate));
    }<commit_msg>Verify that certificate is not a role certificate<commit_after>// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.integration.athenz;

import com.yahoo.vespa.hosted.controller.api.identifiers.AthenzDomain;
import com.yahoo.vespa.hosted.controller.api.identifiers.UserId;

import javax.naming.NamingException;
import javax.naming.ldap.LdapName;
import java.security.cert.X509Certificate;

/**
 * @author bjorncs
 */
public class AthenzUtils {

    private AthenzUtils() {}

    public static final AthenzDomain USER_PRINCIPAL_DOMAIN = new AthenzDomain("user");
    public static final AthenzDomain SCREWDRIVER_DOMAIN = new AthenzDomain("cd.screwdriver.project");
    public static final AthenzService ZMS_ATHENZ_SERVICE = new AthenzService("sys.auth", "zms");

    public static AthenzIdentity createAthenzIdentity(AthenzDomain domain, String identityName) {
        if (domain.equals(USER_PRINCIPAL_DOMAIN)) {
            return AthenzUser.fromUserId(new UserId(identityName));
        } else {
            return new AthenzService(domain, identityName);
        }
    }

    public static AthenzIdentity createAthenzIdentity(String fullName) {
        int domainIdentityNameSeparatorIndex = fullName.lastIndexOf('.');
        if (domainIdentityNameSeparatorIndex == -1
                || domainIdentityNameSeparatorIndex == 0
                || domainIdentityNameSeparatorIndex == fullName.length() - 1) {
            throw new IllegalArgumentException("Invalid Athenz identity: " + fullName);
        }
        AthenzDomain domain = new AthenzDomain(fullName.substring(0, domainIdentityNameSeparatorIndex));
        String identityName = fullName.substring(domainIdentityNameSeparatorIndex + 1, fullName.length());
        return createAthenzIdentity(domain, identityName);
    }

    public static AthenzIdentity createAthenzIdentity(X509Certificate certificate) {
        String commonName = getCommonName(certificate);
        if (isAthenzRoleIdentity(commonName)) {
            throw new IllegalArgumentException("Athenz role certificate not supported");
        }
        return createAthenzIdentity(commonName);
    }

    private static boolean isAthenzRoleIdentity(String commonName) {
        return commonName.contains(":role.");
    }
{'fraction_non_alphanumeric': 0.08476125199908613, 'fraction_numerical': 0.007767877541695225, 'mean_word_length': 3.9468926553672317, 'pattern_counts': {'":': 1, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2149743', 'n_tokens_mistral': 1240, 'n_tokens_neox': 1176, 'n_words': 266}