Search in sources :

Example 1 with SecretException

use of com.netflix.spinnaker.kork.secrets.SecretException in project kork by spinnaker.

the class SecretsManagerSecretEngine method getSecretString.

private byte[] getSecretString(String secretRegion, String secretName, String secretKey) {
    if (!cache.containsKey(secretName)) {
        String secretString = getSecretValue(secretRegion, secretName).getSecretString();
        try {
            Map<String, String> map = mapper.readValue(secretString, Map.class);
            cache.put(secretName, map);
        } catch (JsonProcessingException | IllegalArgumentException e) {
            throw new SecretException(String.format("Failed to parse secret when using AWS Secrets Manager to fetch: [secretName: %s, secretRegion: %s, secretKey: %s]", secretName, secretRegion, secretKey), e);
        }
    }
    return Optional.ofNullable(cache.get(secretName).get(secretKey)).orElseThrow(() -> new SecretException(String.format("Specified key not found in AWS Secrets Manager: [secretName: %s, secretRegion: %s, secretKey: %s]", secretName, secretRegion, secretKey))).getBytes();
}
Also used : SecretException(com.netflix.spinnaker.kork.secrets.SecretException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with SecretException

use of com.netflix.spinnaker.kork.secrets.SecretException in project kork by spinnaker.

the class GcsSecretEngine method downloadRemoteFile.

@Override
protected InputStream downloadRemoteFile(EncryptedSecret encryptedSecret) {
    String bucket = encryptedSecret.getParams().get(STORAGE_BUCKET);
    String objName = encryptedSecret.getParams().get(STORAGE_FILE_URI);
    log.info("Getting contents of object {} from bucket {}", objName, bucket);
    try {
        Storage storage = getStorage();
        return storage.objects().get(bucket, objName).executeMediaAsInputStream();
    } catch (IOException e) {
        throw new SecretException(String.format("Error reading contents of GCS. Bucket: %s, Object: %s.\nError: %s", bucket, objName, e.toString()));
    }
}
Also used : SecretException(com.netflix.spinnaker.kork.secrets.SecretException) Storage(com.google.api.services.storage.Storage) IOException(java.io.IOException)

Example 3 with SecretException

use of com.netflix.spinnaker.kork.secrets.SecretException in project kork by spinnaker.

the class S3SecretEngine method downloadRemoteFile.

@Override
protected InputStream downloadRemoteFile(EncryptedSecret encryptedSecret) throws IOException {
    String region = encryptedSecret.getParams().get(STORAGE_REGION);
    String bucket = encryptedSecret.getParams().get(STORAGE_BUCKET);
    String objName = encryptedSecret.getParams().get(STORAGE_FILE_URI);
    AmazonS3ClientBuilder s3ClientBuilder = AmazonS3ClientBuilder.standard();
    if (this.s3ConfigurationProperties.isPresent()) {
        S3ConfigurationProperties s3ConfigurationProperties = this.s3ConfigurationProperties.get();
        if (!StringUtils.isBlank(s3ConfigurationProperties.getEndpointUrl())) {
            s3ClientBuilder.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(s3ConfigurationProperties.getEndpointUrl(), region));
            s3ClientBuilder.setPathStyleAccessEnabled(s3ConfigurationProperties.isPathStyleAccessEnabled());
        } else {
            throw new SecretException(String.format("Endpoint not found in properties: s3.secret.endpoint-url"));
        }
    } else {
        s3ClientBuilder = s3ClientBuilder.withRegion(region);
    }
    AmazonS3 s3Client = s3ClientBuilder.build();
    try {
        if (!s3Client.doesBucketExistV2(bucket)) {
            throw new SecretException(String.format("S3 Bucket does not exist. Bucket: %s, Region: %s", bucket, region));
        }
        S3Object s3Object = s3Client.getObject(bucket, objName);
        return s3Object.getObjectContent();
    } catch (AmazonS3Exception ex) {
        StringBuilder sb = new StringBuilder("Error reading contents of S3 -- ");
        if (403 == ex.getStatusCode()) {
            sb.append(String.format("Unauthorized access. Check connectivity and permissions to the bucket. -- Bucket: %s, Object: %s, Region: %s.\n" + "Error: %s ", bucket, objName, region, ex.toString()));
        } else if (404 == ex.getStatusCode()) {
            sb.append(String.format("Not found. Does secret file exist? -- Bucket: %s, Object: %s, Region: %s.\nError: %s", bucket, objName, region, ex.toString()));
        } else {
            sb.append(String.format("Error: %s", ex.toString()));
        }
        throw new SecretException(sb.toString(), ex);
    } catch (AmazonClientException ex) {
        throw new SecretException(String.format("Error reading contents of S3. Bucket: %s, Object: %s, Region: %s.\nError: %s", bucket, objName, region, ex.toString()), ex);
    }
}
Also used : SecretException(com.netflix.spinnaker.kork.secrets.SecretException) AmazonS3(com.amazonaws.services.s3.AmazonS3) AmazonS3ClientBuilder(com.amazonaws.services.s3.AmazonS3ClientBuilder) AmazonClientException(com.amazonaws.AmazonClientException) AwsClientBuilder(com.amazonaws.client.builder.AwsClientBuilder) S3Object(com.amazonaws.services.s3.model.S3Object) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Example 4 with SecretException

use of com.netflix.spinnaker.kork.secrets.SecretException in project kork by spinnaker.

the class SecretsManagerSecretEngine method getSecretValue.

protected GetSecretValueResult getSecretValue(String secretRegion, String secretName) {
    AWSSecretsManager client = AWSSecretsManagerClientBuilder.standard().withRegion(secretRegion).build();
    GetSecretValueRequest getSecretValueRequest = new GetSecretValueRequest().withSecretId(secretName);
    try {
        return client.getSecretValue(getSecretValueRequest);
    } catch (AWSSecretsManagerException e) {
        throw new SecretException(String.format("An error occurred when using AWS Secrets Manager to fetch: [secretName: %s, secretRegion: %s]", secretName, secretRegion), e);
    }
}
Also used : SecretException(com.netflix.spinnaker.kork.secrets.SecretException) AWSSecretsManagerException(com.amazonaws.services.secretsmanager.model.AWSSecretsManagerException) AWSSecretsManager(com.amazonaws.services.secretsmanager.AWSSecretsManager) GetSecretValueRequest(com.amazonaws.services.secretsmanager.model.GetSecretValueRequest)

Aggregations

SecretException (com.netflix.spinnaker.kork.secrets.SecretException)4 AmazonClientException (com.amazonaws.AmazonClientException)1 AwsClientBuilder (com.amazonaws.client.builder.AwsClientBuilder)1 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 AmazonS3ClientBuilder (com.amazonaws.services.s3.AmazonS3ClientBuilder)1 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)1 S3Object (com.amazonaws.services.s3.model.S3Object)1 AWSSecretsManager (com.amazonaws.services.secretsmanager.AWSSecretsManager)1 AWSSecretsManagerException (com.amazonaws.services.secretsmanager.model.AWSSecretsManagerException)1 GetSecretValueRequest (com.amazonaws.services.secretsmanager.model.GetSecretValueRequest)1 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 Storage (com.google.api.services.storage.Storage)1 IOException (java.io.IOException)1