Go SDK

Boundary has a Go SDK that sports full coverage of Boundary’s API. This SDK is mostly auto-generated so the patterns are predictable from package to package; for the most part, browsing pkg.go.dev is a great way to get started.

Below, an example walks through using the SDK to authenticate against an auth method or perform recovery workflows. The patterns for creating a resource-typed client are the same across packages.

Authenticating to Boundary with the Go SDK

Authenticating to Boundary starts with an Auth Method. An auth method provides the basic identity delegation needed for Boundary to generate a token for a client. There are two primary methods for authenticating to Boundary:

  1. Via an auth method
  2. Via the recovery KMS workflow

We’ll cover how to authenticate to Boundary via both of these workflows.

Auth Method

This is the most common way for a client to authenticate to Boundary. To demonstrate this, we’ll use the authmethods library to generate a valid token for a client in Go.

For this example, we’re going to use the Password auth method. This example assumes there’s already a valid user and an associated account in Boundary against which the client can authenticate. To simplify this example, we’re assuming you’re running a Boundary instance in dev mode, where the default auth method, login name, and password are pre-configured.

First, we need to create a client from the Boundary API and set the address to reach Boundary:

  1. import "github.com/hashicorp/boundary/api"
  2. // The default address points to the default dev mode address
  3. client, err := api.NewClient(nil)
  4. if err != nil {
  5. return err
  6. }

The authenticate method uses a basic map[string]interface{} to pass credential information, in order to eventually support multiple auth method types. For this example, we assume you’re using the password auth method and so we’re going to tailor a credentials object to pass this data as:

  1. credentials := map[string]interface{}{
  2. "login_name": "admin",
  3. "password": "password",
  4. }

Now let’s create an auth method client using the base client from above:

  1. import "github.com/hashicorp/boundary/api/authmethods"
  2. am := authmethods.NewClient(client)

This creates a shallow copy of the base client. Modifications made to the client via am.ApiClient() will not be reflected in the base client.

The last thing you’ll need is the ID of the auth method in Boundary. You can get this on the CLI with:

  1. $ boundary auth-methods list
  2. Auth Method information:
  3. ID: ampw_1234567890
  4. Description: Provides initial administrative authentication into Boundary
  5. Name: Generated global scope initial auth method
  6. Type: password
  7. Version: 1

Note the ID in the output above, we’re going to use that in the next step.

We can use the credentials object we created to execute Authenticate() on this client:

  1. at, err := am.Authenticate(context.Background(), "ampw_1234567890", credentials)
  2. if err != nil {
  3. return err
  4. }

Lastly, let’s update the original client with the token we got from the Authenticate() call:

  1. // pass this client to any other resource specific API resources
  2. client.SetToken(at.Item.Token)

Putting this all together:

  1. import (
  2. "github.com/hashicorp/boundary/api"
  3. "github.com/hashicorp/boundary/api/authmethods"
  4. )
  5. credentials := map[string]interface{}{
  6. "login_name": "admin",
  7. "password": "password",
  8. }
  9. // The default address points to the default dev mode address
  10. client, err := api.NewClient(nil)
  11. if err != nil {
  12. return err
  13. }
  14. am := authmethods.NewClient(client)
  15. at, err := am.Authenticate(context.Background(), "ampw_1234567890", credentials)
  16. if err != nil {
  17. return err
  18. }
  19. // pass this client to any other resource specific API resources
  20. client.SetToken(at.Item.Token)

Recovery KMS Workflow

The recovery KMS workflow allows you to use a valid KMS configuration to authenticate and authorize calls within the Boundary API. For this example, we’re going to assume you’ve read the above and know how to get a base Boundary API client.

Lets start with a valid KMS configuration for recovery that uses a hard coded AEAD key as the basis. To authenticate with Boundary using this config we’re assuming you have an instance of Boundary that declares this as the recovery KMS in the Boundary controller config as well.

  1. import "github.com/hashicorp/boundary/sdk/wrapper"
  2. const kmsConfig := `
  3. kms "aead" {
  4. purpose = "recovery"
  5. aead_type = "aes-gcm"
  6. key = "8fZBjCUfN0TzjEGLQldGY4+iE9AkOvCfjh7+p0GtRBQ="
  7. key_id = "recovery_kms"
  8. }
  9. `

Now lets use this config to configure our Boundary API client:

  1. w, err := wrapper.GetWrapperFromHcl(kmsConfig, "recovery")
  2. if err != nil {
  3. return err
  4. }
  5. client.SetRecoveryKmsWrapper(w)

The client will now use the recovery KMS wrapper for all authenticated calls (even if you have previously set a token). You can remove it by instantiating a new client, or by passing nil into SetRecoveryKmsWrapper.