All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.akeyless.AkeylessAdminUserImpl Maven / Gradle / Ivy

There is a newer version: 0.0.10
Show newest version
package com.akeyless;

import com.akeyless.api.exceptions.*;
import com.akeyless.api.utils.AkeylessItemType;
import com.akeyless.auth.swagger.model.CredentialsReplyObj;
import com.akeyless.auth.swagger.model.PolicyRules;
import com.akeyless.auth.swagger.model.SetUAMPolicyCredsParams;
import com.akeyless.config.AkeylessUserConfiguration;
import com.akeyless.crypto.ecdsa.ECDSAUtils;
import com.akeyless.crypto.utils.CryptoAlgorithm;
import com.akeyless.exceptions.AkeylessRuntimeException;
import com.akeyless.types.ApiKey;
import com.akeyless.types.UserAccessApiKey;
import com.akeyless.uam.swagger.model.*;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class AkeylessAdminUserImpl extends AkeylessUserImpl  implements AkeylessAdminUser {

    public enum UserUpdateMode {
        UPDATE_KEY_MODE("update_key"),
        UPDATE_EXP_MODE("update_exp"),
        UPDATE_CIDR_MODE("update_cidr");

        private String mode;

        UserUpdateMode(String mode) {
            this.mode = mode;
        }

        public String mode() {
            return mode;
        }
    }

    AkeylessAdminUserImpl(AkeylessUserConfiguration config) throws UnsupportedEncodingException, ApiCommunicationException {
        super(config);
    }

    @Override
    public GetAccountDetailsReplyObj getAccountDetails() throws ApiCommunicationException, CredentialsNotFoundException,
            InvalidCredentialsException, NotFoundException {
        return this.api.getAccountDetails(credsRenewal.getUamCreds());
    }

    @Override
    public void createAESKey(String keyName,
                             CryptoAlgorithm alg,
                             int splitLevel,
                             String userMetadata
    ) throws ApiCommunicationException, InvalidParamException, MissingRequiredParamException, CredentialsNotFoundException,
            InvalidCredentialsException, AlreadyExistsException, UnauthorizedUserException, NoSuchAlgorithmException {
        AkeylessItemType itemType = AkeylessItemType.getTypeByName(alg.getAlgName());
        this.api.createAESKeyItem(credsRenewal.getUamCreds(), keyName, itemType, (long) splitLevel, userMetadata);
    }

    @Override
    public void createRSAKey(String keyName,
                             CryptoAlgorithm alg,
                             int splitLevel,
                             String userMetadata
    ) throws ApiCommunicationException, InvalidParamException, MissingRequiredParamException, CredentialsNotFoundException,
            InvalidCredentialsException, AlreadyExistsException, UnauthorizedUserException, NoSuchAlgorithmException,
            SignatureException, InvalidKeyException {
        AkeylessItemType itemType = AkeylessItemType.getTypeByName(alg.getAlgName());
        this.api.createRSAKeyItem(credsRenewal.getUamCreds(), credsRenewal.getKfmCreds(),
                keyName, itemType, (long) splitLevel, userMetadata);
    }


    @Override
    public void updateKey(String keyName,
                          String newKeyName,
                          String userMetadata
    ) throws ApiCommunicationException, MissingRequiredParamException, InvalidParamException,
            CredentialsNotFoundException, InvalidCredentialsException, AlreadyExistsException {
        api.updateItem(credsRenewal.getUamCreds(), newKeyName, keyName, userMetadata);
    }

    @Override
    public void deleteKey(String keyName) throws ApiCommunicationException,
            MissingRequiredParamException, InvalidParamException, CredentialsNotFoundException,
            InvalidCredentialsException, NotFoundException {
        api.deleteItem(credsRenewal.getUamCreds(), keyName);
    }

    @Override
    public UserAccessApiKey createUser(String userName,
                                       Long accessExpires,
                                       List cidrWhitelist
    ) throws ApiCommunicationException, InvalidParamException, MissingRequiredParamException,
            CredentialsNotFoundException, InvalidCredentialsException, AlreadyExistsException {

        ECPrivateKey prvKey;
        ECPublicKey pubKey;
        try {
            KeyPair keyPair = ECDSAUtils.generateKeyPair("P-256");
            prvKey = (ECPrivateKey) keyPair.getPrivate();
            pubKey = (ECPublicKey) keyPair.getPublic();
        } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
            throw new AkeylessRuntimeException(e);
        }

        String pubKeyEncoded = new String(Base64.encodeBase64(pubKey.getEncoded()));
        String cidrStr = StringUtils.join(cidrWhitelist, ",");
        PolicyRules rules = new PolicyRules().alg("ECDSA_P256_SHA256").key(pubKeyEncoded).cidrWhitelist(cidrStr);

        SetUAMPolicyCredsParams params = new SetUAMPolicyCredsParams().expires(accessExpires).
                        policyRulesType("api_key").rules(rules);
        CredentialsReplyObj setUamPolicyCreds = this.api.setUamPolicyCreds(credsRenewal.getAuthCreds(), params);

        CreateUserReplyObj reply = this.api.createUser(credsRenewal.getUamCreds(),
                setUamPolicyCreds.getCredential(), userName);

        return new UserAccessApiKey(userName, reply.getUserPolicyId(), prvKey);
    }

    @Override
    public GetUserReplyObj getUser(String userName) throws ApiCommunicationException,
            MissingRequiredParamException, InvalidParamException, CredentialsNotFoundException,
            InvalidCredentialsException, NotFoundException {
        return api.getUser(credsRenewal.getUamCreds(), userName);
    }

    @Override
    public GetAccountUsersReplyObj getAccountUsers() throws ApiCommunicationException,
            MissingRequiredParamException, InvalidParamException, CredentialsNotFoundException,
            InvalidCredentialsException, NotFoundException {
        return api.getAccountUsers(credsRenewal.getUamCreds());
    }

    @Override
    public void updateUser(String userName,
                           String newUserName,
                           Long accessExpires,
                           List cidrWhitelist
    ) throws ApiCommunicationException, InvalidParamException, MissingRequiredParamException,
            CredentialsNotFoundException, InvalidCredentialsException, AlreadyExistsException, NotFoundException {

        PolicyRules rules = null;

        List updateModes = new ArrayList<>();
        if(accessExpires != null) {
            updateModes.add(UserUpdateMode.UPDATE_EXP_MODE.mode());
        }
        if(cidrWhitelist != null) {
            updateModes.add(UserUpdateMode.UPDATE_CIDR_MODE.mode());
            rules = new PolicyRules().cidrWhitelist(StringUtils.join(cidrWhitelist, ","));
        }

        SetUAMPolicyCredsParams params = new SetUAMPolicyCredsParams().expires(accessExpires).
                policyRulesType("api_key").rules(rules).updateModes(updateModes);
        CredentialsReplyObj setUamPolicyCreds = this.api.setUamPolicyCreds(credsRenewal.getAuthCreds(), params);

        this.api.updateUser(credsRenewal.getUamCreds(), setUamPolicyCreds.getCredential(), newUserName, userName);
    }

    @Override
    public ApiKey resetUserAccessKey(String userName) throws ApiCommunicationException,
            InvalidParamException, MissingRequiredParamException, CredentialsNotFoundException,
            InvalidCredentialsException, AlreadyExistsException, NotFoundException {

        ECPrivateKey prvKey;
        ECPublicKey pubKey;
        try {
            KeyPair keyPair = ECDSAUtils.generateKeyPair("P-256");
            prvKey = (ECPrivateKey) keyPair.getPrivate();
            pubKey = (ECPublicKey) keyPair.getPublic();
        } catch (NoSuchAlgorithmException | InvalidAlgorithmParameterException e) {
            throw new AkeylessRuntimeException(e);
        }

        String pubKeyEncoded = new String(Base64.encodeBase64(pubKey.getEncoded()));
        PolicyRules rules = new PolicyRules().alg("ECDSA_P256_SHA256").key(pubKeyEncoded);

        SetUAMPolicyCredsParams params = new SetUAMPolicyCredsParams().
                policyRulesType("api_key").rules(rules).updateModes(Arrays.asList(UserUpdateMode.UPDATE_KEY_MODE.mode()));
        CredentialsReplyObj setUamPolicyCreds = this.api.setUamPolicyCreds(credsRenewal.getAuthCreds(), params);

        this.api.updateUser(credsRenewal.getUamCreds(), setUamPolicyCreds.getCredential(), userName, userName);
        return new ApiKey(prvKey);
    }

    @Override
    public void deleteUser(String userName) throws ApiCommunicationException,
            MissingRequiredParamException, InvalidParamException, CredentialsNotFoundException,
            InvalidCredentialsException, NotFoundException {
        api.deleteUser(credsRenewal.getUamCreds(), userName);
    }

    @Override
    public void createRole(String roleName,
                           String roleAction,
                           String comment
    ) throws ApiCommunicationException, MissingRequiredParamException, InvalidParamException,
            CredentialsNotFoundException, InvalidCredentialsException, AlreadyExistsException {
        api.createRole(credsRenewal.getUamCreds(), roleName, roleAction, comment);
    }

    @Override
    public GetRoleReplyObj getRole(String roleName
    ) throws ApiCommunicationException, MissingRequiredParamException, InvalidParamException,
            CredentialsNotFoundException, InvalidCredentialsException, NotFoundException {
        return api.getRole(credsRenewal.getUamCreds(), roleName);
    }

    @Override
    public GetAccountRolesReplyObj getAccountRoles() throws ApiCommunicationException,
            MissingRequiredParamException, InvalidParamException, CredentialsNotFoundException,
            InvalidCredentialsException, NotFoundException {
        return api.getAccountRoles(credsRenewal.getUamCreds());
    }

    @Override
    public void updateRole(String roleName,
                           String newRoleName,
                           String roleAction,
                           String comment
    ) throws ApiCommunicationException, MissingRequiredParamException, InvalidParamException,
            CredentialsNotFoundException, InvalidCredentialsException, NotFoundException, AlreadyExistsException {
        api.updateRole(credsRenewal.getUamCreds(), newRoleName, roleName, roleAction, comment);
    }

    @Override
    public void deleteRole(String roleName
    ) throws ApiCommunicationException, MissingRequiredParamException, InvalidParamException,
            CredentialsNotFoundException, InvalidCredentialsException, NotFoundException {
        api.deleteRole(credsRenewal.getUamCreds(), roleName);
    }

    @Override
    public void createRoleItemAssoc(String roleName,
                                    String itemName
    ) throws ApiCommunicationException, MissingRequiredParamException, InvalidParamException,
            CredentialsNotFoundException, InvalidCredentialsException, NotFoundException {
        api.createRoleItemAssoc(credsRenewal.getUamCreds(), roleName, itemName);
    }

    @Override
    public void createRoleUserAssoc(String roleName,
                                    String userName
    ) throws ApiCommunicationException, MissingRequiredParamException, InvalidParamException,
            CredentialsNotFoundException, InvalidCredentialsException, NotFoundException {
        api.createRoleUserAssoc(credsRenewal.getUamCreds(), roleName, userName);
    }

    @Override
    public void deleteRoleItemAssoc(String roleName,
                                    String itemName
    ) throws ApiCommunicationException, MissingRequiredParamException, InvalidParamException,
            CredentialsNotFoundException, InvalidCredentialsException, NotFoundException {
        api.deleteRoleItemAssoc(credsRenewal.getUamCreds(), roleName, itemName);
    }

    @Override
    public void deleteRoleUserAssoc(String roleName,
                                    String userName
    ) throws ApiCommunicationException, MissingRequiredParamException, InvalidParamException,
            CredentialsNotFoundException, InvalidCredentialsException, NotFoundException {
        api.deleteRoleUserAssoc(credsRenewal.getUamCreds(), roleName, userName);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy