|
1 | | -# golang-sdk |
| 1 | +# Phase Secrets Management SDK |
| 2 | + |
| 3 | +The Phase Secrets SDK provides a Go package for managing secrets in your application environments using the Phase service. This SDK let's you create, retrieve, update, and delete secrets, with end-to-end encryption with just a few lines of code. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +To start using the Phase SDK in your Go project, install it using `go get`: |
| 8 | + |
| 9 | +```bash |
| 10 | +go get github.com/phasehq/golang-sdk/phase |
| 11 | +``` |
| 12 | + |
| 13 | +Make sure to import the SDK in your Go files: |
| 14 | + |
| 15 | +```go |
| 16 | +import "github.com/phasehq/golang-sdk/phase" |
| 17 | +``` |
| 18 | + |
| 19 | +## Configuration |
| 20 | + |
| 21 | +Before you can interact with the Phase service, you need to initialize the SDK with your service token and the host information. |
| 22 | + |
| 23 | +```go |
| 24 | +package main |
| 25 | + |
| 26 | +import ( |
| 27 | + "log" |
| 28 | + "github.com/phasehq/golang-sdk/phase" |
| 29 | +) |
| 30 | + |
| 31 | +func main() { |
| 32 | + serviceToken := "pss_service:v1:....." |
| 33 | + host := "https://console.phase.dev" // Change this for a self hosted instance of Phase |
| 34 | + debug := false |
| 35 | + |
| 36 | + phaseClient := phase.Init(serviceToken, host, debug) |
| 37 | + if phaseClient == nil { |
| 38 | + log.Fatal("Failed to initialize Phase client") |
| 39 | + } |
| 40 | +} |
| 41 | +``` |
| 42 | + |
| 43 | +## Creating a Secret |
| 44 | + |
| 45 | +To create new secrets, define key-value pairs, specify the environment and application name, and optionally set paths for each key. |
| 46 | + |
| 47 | +```go |
| 48 | +opts := phase.CreateSecretsOptions{ |
| 49 | + KeyValuePairs: []map[string]string{ |
| 50 | + {"API_KEY": "api_secret"}, |
| 51 | + }, |
| 52 | + EnvName: "Production", |
| 53 | + AppName: "MyApp", |
| 54 | + SecretPath: map[string]string{"API_KEY": "/api/keys"}, // Optional, default path: / |
| 55 | +} |
| 56 | + |
| 57 | +err := phaseClient.Create(opts) |
| 58 | +if err != nil { |
| 59 | + log.Fatalf("Failed to create secret: %v", err) |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +## Retrieving a Secret |
| 64 | + |
| 65 | +To retrieve a secret, provide the environment name, application name, key to find, and optionally a tag and path. |
| 66 | + |
| 67 | +```go |
| 68 | +getOpts := phase.GetSecretOptions{ |
| 69 | + EnvName: "Production", |
| 70 | + AppName: "MyApp", |
| 71 | + KeyToFind: "API_KEY", |
| 72 | +} |
| 73 | + |
| 74 | +secret, err := phaseClient.Get(getOpts) |
| 75 | +if err != nil { |
| 76 | + log.Fatalf("Failed to get secret: %v", err) |
| 77 | +} else { |
| 78 | + log.Printf("Secret: %+v", secret) |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +## Updating a Secret |
| 83 | + |
| 84 | +To update an existing secret, provide the new value along with the environment name, application name, key, and optionally the path. |
| 85 | + |
| 86 | +```go |
| 87 | +updateOpts := phase.SecretUpdateOptions{ |
| 88 | + EnvName: "Production", |
| 89 | + AppName: "MyApp", |
| 90 | + Key: "API_KEY", |
| 91 | + Value: "my_updated_api_secret", |
| 92 | + SecretPath: "/api/keys", // Optional, default path: / |
| 93 | +} |
| 94 | + |
| 95 | +err := phaseClient.Update(updateOpts) |
| 96 | +if err != nil { |
| 97 | + log.Fatalf("Failed to update secret: %v", err) |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +## Deleting a Secret |
| 102 | + |
| 103 | +To delete a secret, specify the environment name, application name, key to delete, and optionally the path. |
| 104 | + |
| 105 | +```go |
| 106 | +deleteOpts := phase.DeleteSecretOptions{ |
| 107 | + EnvName: "Production", |
| 108 | + AppName: "MyApp", |
| 109 | + KeyToDelete: "API_KEY", |
| 110 | + SecretPath: "/api/keys", // Optional, default path: / |
| 111 | +} |
| 112 | + |
| 113 | +err := phaseClient.Delete(deleteOpts) |
| 114 | +if err != nil { |
| 115 | + log.Fatalf("Failed to delete secret: %v", err) |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +For more information and advanced usage, refer to the official Phase documentation. |
| 120 | + |
| 121 | +--- |
0 commit comments