secretmanager

package module
v0.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 7, 2025 License: Apache-2.0 Imports: 7 Imported by: 0

README

sacloud/secretmanager-api-go

Go言語向けのさくらのクラウド シークレットマネージャ APIライブラリ

シークレットマネージャ ドキュメント: https://manual.sakura.ad.jp/cloud/appliance/secretsmanager/index.html

概要

sacloud/secretmanager-api-goはさくらのクラウド KMS APIをGo言語から利用するためのAPIライブラリです。

package main

import (
    "context"
    "fmt"

    sm "github.com/sacloud/secretmanager-api-go"
    v1 "github.com/sacloud/secretmanager-api-go/apis/v1"
)

func main() {
	client, err := sm.NewClient()
	if err != nil {
		panic(err)
	}

	ctx := context.Background()
	keyId := os.Getenv("SAKURACLOUD_KMS_KEY_ID") // コンパネやkms-api-goなどで取得
	vaultOp := sm.NewVaultOp(client)

	vault, err := vaultOp.Create(ctx, v1.CreateVault{
		Name:        "app1_vault",
		Description: v1.NewOptString("vault for app1"),
		KmsKeyID:    keyId,
		Tags:        []string{"app1"},
	})
	if err != nil {
		panic(err)
	}

	secOp := sm.NewSecretOp(client, vault.ID)

	resCreate, err := secOp.Create(ctx, v1.CreateSecret{
		Name:  "secret1",
		Value: "Secret Value 1",
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("version: " + strconv.Itoa(resCreate.LatestVersion))

	resList, err := secOp.List(ctx)
	if err != nil {
		panic(err)
	}

	for _, sec := range resList {
		fmt.Println("name: " + sec.Name + ", version: " + strconv.Itoa(sec.LatestVersion))
	}

	resUn, err := secOp.Unveil(ctx, v1.Unveil{
		Name: "secret1",
		//Version: v1.NewOptNilInt(1), // Versionを指定して取得も可能
	})
	if err != nil {
		panic(err)
	}
	fmt.Println("value: " + resUn.Value)
}

vaults_test.go / secrets_test.go も参照。

クライアントに設定を渡す

api-client-go にある ClientParams オプションを WithXXX で指定可能です。

// API keysをコードから指定する例
import (
	// ...

	client "github.com/sacloud/api-client-go"
	sm "github.com/sacloud/secretmanager-api-go"
)

func main() {
	client, err := sm.NewClient(client.WithApiKeys("your-token", "your-token-secret"))
	// ...
}

⚠ v1.0に達するまでは互換性のない形で変更される可能性がありますのでご注意ください。

ogenによるコード生成

以下のコマンドを実行

$ go get -tool github.com/ogen-go/ogen/cmd/ogen@latest
$ go tool ogen -package v1 -target apis/v1 -clean -config ogen-config.yaml ./openapi/openapi-fixed.json

License

secretmanager-api-go Copyright (C) 2025- The sacloud/secretmanager-api-go authors. This project is published under Apache 2.0 License.

Documentation

Index

Constants

View Source
const DefaultAPIRootURL = "https://secure.sakura.ad.jp/cloud/zone/tk1a/api/cloud/1.1"

DefaultAPIRootURL デフォルトのAPIルートURL

View Source
const Version = "0.2.1"

Variables

View Source
var UserAgent = fmt.Sprintf(
	"secretmanager-api-go/%s (%s/%s; +https://github.com/sacloud/secretmanager-api-go) %s",
	Version,
	runtime.GOOS,
	runtime.GOARCH,
	client.DefaultUserAgent,
)

UserAgent APIリクエスト時のユーザーエージェント

Functions

func NewClient

func NewClient(params ...client.ClientParam) (*v1.Client, error)

func NewClientWithApiUrl

func NewClientWithApiUrl(apiUrl string, params ...client.ClientParam) (*v1.Client, error)

Types

type DummySecuritySource

type DummySecuritySource struct {
	Username string
	Password string
}

SecuritySourceはOpenAPI定義で使用されている認証のための仕組み。api-client-goが処理するので、ogen用はダミーで誤魔化す

func (DummySecuritySource) BasicAuth

func (ss DummySecuritySource) BasicAuth(ctx context.Context, operationName v1.OperationName) (v1.BasicAuth, error)

type Error added in v0.2.0

type Error struct {
	// contains filtered or unexported fields
}

func NewAPIError added in v0.2.1

func NewAPIError(method string, code int, err error) *Error

func NewError added in v0.2.0

func NewError(msg string, err error) *Error

func (*Error) Error added in v0.2.0

func (e *Error) Error() string

func (*Error) Unwrap added in v0.2.0

func (e *Error) Unwrap() error

type SecretAPI

type SecretAPI interface {
	List(ctx context.Context) ([]v1.Secret, error)
	// Read(ctx context.Context, id string) (*v1.Secret, error)
	Create(ctx context.Context, request v1.CreateSecret) (*v1.Secret, error)
	Update(ctx context.Context, request v1.CreateSecret) (*v1.Secret, error)
	Delete(ctx context.Context, request v1.DeleteSecret) error
	Unveil(ctx context.Context, request v1.Unveil) (*v1.Unveil, error)
}

SecretAPIはSecretの操作をCRUD+Lで行うためのインターフェース. READは未実装

func NewSecretOp

func NewSecretOp(client *v1.Client, id string) SecretAPI

type VaultAPI

type VaultAPI interface {
	List(ctx context.Context) ([]v1.Vault, error)
	Read(ctx context.Context, id string) (*v1.Vault, error)
	Create(ctx context.Context, request v1.CreateVault) (*v1.CreateVault, error)
	Update(ctx context.Context, id string, request v1.Vault) (*v1.Vault, error)
	Delete(ctx context.Context, id string) error
}

VaultAPIはVaultの操作をCRUD+Lで行うためのインターフェース

func NewVaultOp

func NewVaultOp(client *v1.Client) VaultAPI

Directories

Path Synopsis
apis
v1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL