Skip to content

IBM/secrets-manager-java-sdk

Repository files navigation

IBM Cloud Secrets Manager Java SDK

A Java client library to interact with the IBM Cloud® Secrets Manager APIs.

Table of Contents

Overview

The IBM Cloud Secrets Manager Java SDK allows developers to programmatically interact with the following IBM Cloud services:

Service name Imported class name
Secrets Manager SecretsManager

Prerequisites

Installation

Maven
<dependency>
    <groupId>com.ibm.cloud</groupId>
    <artifactId>secrets-manager</artifactId>
    <version>2.0.11</version>
</dependency>
Gradle
'com.ibm.cloud:secrets-manager:2.0.11'

Authentication

Secrets Manager uses token-based Identity and Access Management (IAM) authentication.

With IAM authentication, you supply an API key that is used to generate an access token. Then, the access token is included in each API request to Secrets Manager. Access tokens are valid for a limited amount of time and must be regenerated.

Authentication for this SDK is accomplished by using IAM authenticators. Import authenticators from com.ibm.cloud.sdk.core.security.

Examples

Programmatic credentials

import com.ibm.cloud.sdk.core.security.IamAuthenticator;
...
IamAuthenticator iamAuthenticator = new IamAuthenticator.Builder()
        .apikey("<IBM_CLOUD_API_KEY>")
        .build();

To learn more about IAM authenticators and how to use them in your Java application, see the IBM Java SDK Core documentation.

Using the SDK

Basic usage

  • Use the setServiceUrl method to set the endpoint URL that is specific to your Secrets Manager service instance. To find your endpoint URL, you can copy it from the Endpoints page in the Secrets Manager UI.

Examples

Construct a service client and use it to create and retrieve a secret from your Secrets Manager instance.

Here's an example main.java class file:

import com.ibm.cloud.secrets_manager_sdk.secrets_manager.v2.SecretsManager;
import com.ibm.cloud.secrets_manager_sdk.secrets_manager.v2.model.*;
import com.ibm.cloud.sdk.core.http.Response;
import com.ibm.cloud.sdk.core.security.IamAuthenticator;

import java.util.Collections;

public class main {

    protected static SecretsManager sm;
    protected static IamAuthenticator iamAuthenticator;

    public static void main(String[] args) { 
        iamAuthenticator = new IamAuthenticator.Builder()
              .apikey("IBM_CLOUD_API_KEY")
              .build();
        sm = new SecretsManager("My Secrets-Manager service", iamAuthenticator);
        sm.setServiceUrl("SERVICE_URL");

        // create arbitrary secret
        ArbitrarySecretPrototype arbitrarySecretResource = new ArbitrarySecretPrototype.Builder()
                        .name("example-arbitrary-secret")
                        .description("Extended description for this secret.")
                        .payload("secret-data")
                        .secretType("arbitrary")
                        .build();
        CreateSecretOptions createSecretOptions = new CreateSecretOptions.Builder()
                .secretPrototype(arbitrarySecretResource)
                .build();
        Response<Secret> createResp = sm.createSecret(createSecretOptions).execute();

        String secretId = createResp.getResult().getId();

        // get arbitrary secret
        GetSecretOptions getSecretOptions = new GetSecretOptions.Builder()
                .id(secretId)
                .build();
        Response<Secret> getResp = sm.getSecret(getSecretOptions).execute();

        String secretPayload = (String) getResp.getResult().getPayload();

        System.out.println("The arbitrary secret payload is: " + secretPayload);
    }

}

Replace the IBM_CLOUD_API_KEY and SERVICE_URL values. Then run your application. You should see the payload of the arbitrary secret that was created.

For more information and IBM Cloud SDK usage examples for Java, see the IBM Cloud SDK Common documentation.

Questions

If you're having difficulties using this SDK, you can ask questions about this project by using Stack Overflow. Be sure to include the ibm-cloud and ibm-secrets-manager tags.

You can also check out the Secrets Manager documentation and API reference for more information about the service.

Issues

If you encounter an issue with the project, you're welcome to submit a bug report to help us improve.

Contributing

For general contribution guidelines, see CONTRIBUTING.

License

This SDK project is released under the Apache 2.0 license. The license's full text can be found in LICENSE.