Git Product home page Git Product logo

golang-sdk's Introduction

Phase Secrets Management SDK

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.

Features:

  • End-to-end encrypting secret CRUD
  • Cross and local env secret referencing
  • Built in handling of rate limiting

Secret referencing syntax:

Reference syntax Environment Path Secret Key Being Referenced Description
${KEY} same environment / KEY Local reference in the same environment and path root (/).
${staging.DEBUG} dev / (root of staging environment) DEBUG Cross-environment reference to a secret at the root (/).
${prod./frontend/SECRET_KEY} prod /frontend/ SECRET_KEY Cross-environment reference to a secret in a specific path.
${/backend/payments/STRIPE_KEY} same environment /backend/payments/ STRIPE_KEY Local reference with a specified path within the same environment.

Installation

This SDK uses the sodium package to perform cryptographic operations, on most system you will need to install the libsodium library as a system dependency. Here's how you can install libsodium or its development packages on different platforms, including macOS, Ubuntu, Debian, Arch Linux, Alpine Linux, and Windows.

macOS

brew install libsodium

Fedora

sudo dnf install libsodium-devel

Ubuntu and Debian

sudo apt-get update && sudo apt-get install libsodium-dev

Arch Linux

sudo pacman -Syu libsodium

Alpine Linux

sudo apk add libsodium-dev

Windows

On Windows, the process is a bit different due to the variety of development environments. However, you can download pre-built binaries from the official libsodium GitHub releases page. Choose the appropriate version for your system architecture (e.g., Win32 or Win64), download it, and follow the instructions included to integrate libsodium with your development environment. For development with Visual Studio, you'll typically include the header files and link against the libsodium.lib or libsodium.dll file.

If you're using a package manager like vcpkg or chocolatey, you can also find libsodium packages available for installation:

  • Using vcpkg:
    vcpkg install libsodium
  • Using chocolatey:
    choco install libsodium

Remember, after installing the library, you might need to configure your project or environment variables to locate the libsodium libraries correctly, especially on Windows.

Next, start using the Phase SDK in your Go project, install it using go get:

go get github.com/phasehq/golang-sdk/phase

Make sure to import the SDK in your Go files:

import "github.com/phasehq/golang-sdk/phase"

Configuration

Before you can interact with the Phase service, you need to initialize the SDK with your service token and the host information.

package main

import (
    "log"
    "github.com/phasehq/golang-sdk/phase"
)

func main() {
    serviceToken := "pss_service:v1:....."
    host := "https://console.phase.dev" // Change this for a self hosted instance of Phase
    debug := false

    phaseClient := phase.Init(serviceToken, host, debug)
    if phaseClient == nil {
        log.Fatal("Failed to initialize Phase client")
    }
}

Creating a Secret

To create new secrets, define key-value pairs, specify the environment and application name, and optionally set paths for each key.

opts := phase.CreateSecretsOptions{
    KeyValuePairs: []map[string]string{
        {"API_KEY": "api_secret"},
    },
    EnvName:    "Production",
    AppName:    "MyApp",
    SecretPath: map[string]string{"API_KEY": "/api/keys"}, // Optional, default path: /
}

err := phaseClient.Create(opts)
if err != nil {
    log.Fatalf("Failed to create secret: %v", err)
}

Retrieving a Secret

To retrieve a secret, provide the environment name, application name, key to find, and optionally a tag and path.

getOpts := phase.GetSecretOptions{
    EnvName:   "Production",
    AppName:   "MyApp",
    KeyToFind: "API_KEY",
}

secret, err := phaseClient.Get(getOpts)
if err != nil {
    log.Fatalf("Failed to get secret: %v", err)
} else {
    log.Printf("Secret: %+v", secret)
}

Updating a Secret

To update an existing secret, provide the new value along with the environment name, application name, key, and optionally the path.

updateOpts := phase.SecretUpdateOptions{
    EnvName:    "Production",
    AppName:    "MyApp",
    Key:        "API_KEY",
    Value:      "my_updated_api_secret",
    SecretPath: "/api/keys", // Optional, default path: /
}

err := phaseClient.Update(updateOpts)
if err != nil {
    log.Fatalf("Failed to update secret: %v", err)
}

Deleting a Secret

To delete a secret, specify the environment name, application name, key to delete, and optionally the path.

deleteOpts := phase.DeleteSecretOptions{
    EnvName:     "Production",
    AppName:     "MyApp",
    KeyToDelete: "API_KEY",
    SecretPath:  "/api/keys", // Optional, default path: /
}

err := phaseClient.Delete(deleteOpts)
if err != nil {
    log.Fatalf("Failed to delete secret: %v", err)
}

For more information and advanced usage, refer to the Phase Docs.


golang-sdk's People

Contributors

nimish-ks avatar rohan-chaturvedi avatar

Stargazers

Rebaz Raouf avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.