Skip to content

Commit

Permalink
[JetpackAPIStore] use apiClient (#238)
Browse files Browse the repository at this point in the history
## Summary

This uses the new `apiClient`, which has the benefit of managing the
Authorization
header from the token, as well as creating the SecretsService client
just once.

I chose to avoid re-defining the SecretsService's methods again. Would
prefer
benefitting from the codegen we get and directly using that.

## How was it tested?

`envsec ls` and `envsec set` work as before
  • Loading branch information
savil authored Dec 20, 2023
1 parent 944b419 commit 14ba2cf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
7 changes: 4 additions & 3 deletions envsec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/pkg/errors"
"go.jetpack.io/envsec/internal/build"
"go.jetpack.io/pkg/auth/session"
"go.jetpack.io/pkg/envvar"
)

Expand Down Expand Up @@ -63,7 +64,7 @@ func NewStore(ctx context.Context, config Config) (Store, error) {
case *SSMConfig:
return newSSMStore(ctx, config)
case *JetpackAPIConfig:
return newJetpackAPIStore(config), nil
return newJetpackAPIStore(ctx, config), nil
default:
return nil, errors.Errorf("unsupported store type: %T", config)
}
Expand Down Expand Up @@ -116,7 +117,7 @@ func (c *SSMConfig) hasDefaultPaths() bool {

type JetpackAPIConfig struct {
host string
token string
token *session.Token
}

// JetpackAPIStore implements interface Config (compile-time check)
Expand All @@ -126,7 +127,7 @@ func (c *JetpackAPIConfig) IsEnvStoreConfig() bool {
return true
}

func NewJetpackAPIConfig(token string) *JetpackAPIConfig {
func NewJetpackAPIConfig(token *session.Token) *JetpackAPIConfig {
return &JetpackAPIConfig{
envvar.Get("ENVSEC_JETPACK_API_HOST", build.JetpackAPIHost()),
token,
Expand Down
42 changes: 16 additions & 26 deletions jetpack_api_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,30 @@ package envsec

import (
"context"
"net/http"

"connectrpc.com/connect"
"go.jetpack.io/pkg/api"
secretsv1alpha1 "go.jetpack.io/pkg/api/gen/priv/secrets/v1alpha1"
"go.jetpack.io/pkg/api/gen/priv/secrets/v1alpha1/secretsv1alpha1connect"
)

type JetpackAPIStore struct {
config *JetpackAPIConfig
client secretsv1alpha1connect.SecretsServiceClient
}

// JetpackAPIStore implements interface Store (compile-time check)
var _ Store = (*JetpackAPIStore)(nil)

func newJetpackAPIStore(config *JetpackAPIConfig) *JetpackAPIStore {
return &JetpackAPIStore{config: config}
func newJetpackAPIStore(ctx context.Context, config *JetpackAPIConfig) *JetpackAPIStore {
return &JetpackAPIStore{
client: api.NewClient(ctx, config.host, config.token).SecretsService(),
}
}

func (j JetpackAPIStore) List(ctx context.Context, envID EnvID) ([]EnvVar, error) {
resp, err := j.client().ListSecrets(
resp, err := j.client.ListSecrets(
ctx,
newRequest(&secretsv1alpha1.ListSecretsRequest{ProjectId: envID.ProjectID}, j.config.token),
connect.NewRequest(&secretsv1alpha1.ListSecretsRequest{ProjectId: envID.ProjectID}),
)
if err != nil {
return nil, err
Expand All @@ -43,8 +45,8 @@ func (j JetpackAPIStore) List(ctx context.Context, envID EnvID) ([]EnvVar, error
}

func (j JetpackAPIStore) Set(ctx context.Context, envID EnvID, name string, value string) error {
_, err := j.client().PatchSecret(
ctx, newRequest(
_, err := j.client.PatchSecret(
ctx, connect.NewRequest(
&secretsv1alpha1.PatchSecretRequest{
ProjectId: envID.ProjectID,
Secret: &secretsv1alpha1.Secret{
Expand All @@ -54,7 +56,6 @@ func (j JetpackAPIStore) Set(ctx context.Context, envID EnvID, name string, valu
},
},
},
j.config.token,
),
)
return err
Expand All @@ -80,8 +81,8 @@ func (j JetpackAPIStore) SetAll(ctx context.Context, envID EnvID, values map[str
)
}

_, err := j.client().Batch(
ctx, newRequest(&secretsv1alpha1.BatchRequest{Actions: patchActions}, j.config.token),
_, err := j.client.Batch(
ctx, connect.NewRequest(&secretsv1alpha1.BatchRequest{Actions: patchActions}),
)
return err
}
Expand Down Expand Up @@ -116,14 +117,13 @@ func (j JetpackAPIStore) GetAll(ctx context.Context, envID EnvID, names []string
}

func (j JetpackAPIStore) Delete(ctx context.Context, envID EnvID, name string) error {
_, err := j.client().DeleteSecret(
ctx, newRequest(
_, err := j.client.DeleteSecret(
ctx, connect.NewRequest(
&secretsv1alpha1.DeleteSecretRequest{
ProjectId: envID.ProjectID,
SecretName: name,
Environments: []string{envID.EnvName},
},
j.config.token,
),
)
return err
Expand All @@ -145,18 +145,8 @@ func (j JetpackAPIStore) DeleteAll(ctx context.Context, envID EnvID, names []str
)
}

_, err := j.client().Batch(
ctx, newRequest(&secretsv1alpha1.BatchRequest{Actions: deleteActions}, j.config.token),
_, err := j.client.Batch(
ctx, connect.NewRequest(&secretsv1alpha1.BatchRequest{Actions: deleteActions}),
)
return err
}

func (j JetpackAPIStore) client() secretsv1alpha1connect.SecretsServiceClient {
return secretsv1alpha1connect.NewSecretsServiceClient(http.DefaultClient, j.config.host)
}

func newRequest[T any](message *T, token string) *connect.Request[T] {
req := connect.NewRequest(message)
req.Header().Set("Authorization", "Bearer "+token)
return req
}
2 changes: 1 addition & 1 deletion pkg/envcli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (f *configFlags) genConfig(cmd *cobra.Command) (*CmdConfig, error) {
return nil, errors.WithStack(err)
}
} else {
store, err = envsec.NewStore(ctx, envsec.NewJetpackAPIConfig(tok.AccessToken))
store, err = envsec.NewStore(ctx, envsec.NewJetpackAPIConfig(tok))
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down

0 comments on commit 14ba2cf

Please sign in to comment.