Go - Secret.Latest()

Returns a reference to the latest version of a secret, regardless of that version's ID.

import (
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  secret, err := nitric.NewSecret("secret-name").With(nitric.SecretAccessing)
  if err != nil {
    return
  }

  latest := secret.Latest()

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Notes

Latest() is most useful when you always want the most recent secret values from the secrets manager. Database credentials and API keys are good examples of secrets where the latest value is usually what you want.

For symmetric encryption, you'll need to retrieve the version of the secret used to encrypt a value when you try to decrypt it again. In those cases Latest() isn't a good choice, use version() instead.

Examples

Get a reference to the latest secret version

import (
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  secret, err := nitric.NewSecret("secret-name").With(nitric.SecretAccessing)
  if err != nil {
    return
  }

  latest := secret.Latest()

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}

Access the latest value of a secret

import (
  "fmt"

  "github.com/nitrictech/go-sdk/nitric"
)

func main() {
  secret, err := nitric.NewSecret("secret-name").With(nitric.SecretAccessing)
  if err != nil {
    return
  }

  value, err := secret.Latest().Access(context.TODO())
  if err != nil {
    return
  }

  fmt.Println(value.AsString())

  if err := nitric.Run(); err != nil {
    fmt.Println(err)
  }
}