public static void populateSecret()

in code/src/main/java/com/amazonaws/mariadbblog/DbClient_QueryClusterNodes.java [24:89]


    public static void populateSecret() {
        String secretName = "db-blog";
        String region = "us-west-2";

        // Create a Secrets Manager client
        AWSSecretsManager client  = AWSSecretsManagerClientBuilder.standard()
                .withRegion(region)
                .build();

        // In this sample we only handle the specific exceptions for the 'GetSecretValue' API.
        // See https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
        // We rethrow the exception by default.

        String secret = null;

        try {
            GetSecretValueResult getSecretValueResult = client.getSecretValue(new GetSecretValueRequest()
                    .withSecretId(secretName));

            // Decrypts secret using the associated KMS CMK.
            // Depending on whether the secret is a string or binary, one of these fields will be populated.
            if (getSecretValueResult.getSecretString() != null) {
                secret = getSecretValueResult.getSecretString();
            } else {
                secret = new String(Base64.getDecoder().decode(getSecretValueResult.getSecretBinary()).array());
            }

            //System.out.println(" Secret values...");
           // System.out.println(secret);

            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(secret);
            USER = jsonNode.get("username").asText();
            PASS = jsonNode.get("password").asText();
            DB_URL = String.format(
                    "jdbc:mariadb:aurora//%s:%s/" + "?allowMultiQueries=true&useSSL=false",
                    jsonNode.get("host").asText(),
                    jsonNode.get("port").asText());
        } catch (DecryptionFailureException e) {
            // Secrets Manager can't decrypt the protected secret text using the provided KMS key.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw e;
        } catch (InternalServiceErrorException e) {
            // An error occurred on the server side.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw e;
        } catch (InvalidParameterException e) {
            // You provided an invalid value for a parameter.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw e;
        } catch (InvalidRequestException e) {
            // You provided a parameter value that is not valid for the current state of the resource.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw e;
        } catch (ResourceNotFoundException e) {
            // We can't find the resource that you asked for.
            // Deal with the exception here, and/or rethrow at your discretion.
            throw e;
        } catch (JsonProcessingException e) {
            // We can't deserialize the secret String.
            // Deal with the exception here, and/or rethrow at your discretion.
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }