Java Code Examples for com.cloudbees.plugins.credentials.CredentialsStore#save()

The following examples show how to use com.cloudbees.plugins.credentials.CredentialsStore#save() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserSSHKeyManager.java    From blueocean-plugin with MIT License 5 votes vote down vote up
/**
 * Gets the existing generated SSH key for the user or creates one and
 * returns it in the user's credential store
 * @param user owner of the key
 * @return the user's personal private key
 */
public static @Nonnull BasicSSHUserPrivateKey getOrCreate(@Nonnull User user) {
    Preconditions.checkNotNull(user);

    CredentialsStore store = getUserStore(user);
    if(store == null){
        throw new ServiceException.ForbiddenException(String.format("Logged in user: %s doesn't have writable credentials store", user.getId()));
    }
    // try to find the right key
    for (Credentials cred : store.getCredentials(getDomain(store))) {
        if (cred instanceof BasicSSHUserPrivateKey) {
            BasicSSHUserPrivateKey sshKey = (BasicSSHUserPrivateKey)cred;
            if (BLUEOCEAN_GENERATED_SSH_KEY_ID.equals(sshKey.getId())) {
                return sshKey;
            }
        }
    }
    // if none found, create one
    try {
        // create one!
        String privateKey = SSHKeyUtils.generateKey(KEY_SIZE).trim();
        BasicSSHUserPrivateKey.DirectEntryPrivateKeySource keySource = new BasicSSHUserPrivateKey.DirectEntryPrivateKeySource(privateKey);
        BasicSSHUserPrivateKey key = new BasicSSHUserPrivateKey(CredentialsScope.USER, BLUEOCEAN_GENERATED_SSH_KEY_ID, user.getId(), keySource, null, BLUEOCEAN_GENERATED_SSH_KEY_ID);
        store.addCredentials(getDomain(store), key);
        store.save();
        return key;
    } catch (IOException ex) {
        throw new ServiceException.UnexpectedErrorException("Failed to create the private key", ex);
    }
}
 
Example 2
Source File: UserSSHKeyManager.java    From blueocean-plugin with MIT License 5 votes vote down vote up
/**
 * Resets the user's generated key by deleting it and creating a new one
 * @param user user to reset a key for
 */
public static void reset(@Nonnull User user) {
    Preconditions.checkNotNull(user);

    try {
        // create one!
        CredentialsStore store = getUserStore(user);
        if(store == null){
            throw new ServiceException.ForbiddenException(String.format("Logged in user: %s doesn't have writable credentials store", user.getId()));
        }

        Credentials key = null;
        // try to find the key
        for (Credentials cred : store.getCredentials(getDomain(store))) {
            if (cred instanceof BasicSSHUserPrivateKey) {
                BasicSSHUserPrivateKey sshKey = (BasicSSHUserPrivateKey)cred;
                if (BLUEOCEAN_GENERATED_SSH_KEY_ID.equals(sshKey.getId())) {
                    key = sshKey;
                    break;
                }
            }
        }
        if (key != null) {
            store.removeCredentials(getDomain(store), key);
            store.save();
        }
    } catch (IOException ex) {
        throw new ServiceException.UnexpectedErrorException("Unable to reset the user's key", ex);
    }
}