Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[envsec] Add List function to envsec library #252

Merged
merged 3 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 0 additions & 135 deletions envsec/envsec.go

This file was deleted.

8 changes: 4 additions & 4 deletions envsec/pkg/awsfed/awsfed.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/cognitoidentity"
"github.com/aws/aws-sdk-go-v2/service/cognitoidentity/types"
"github.com/pkg/errors"
"go.jetpack.io/envsec"
"go.jetpack.io/envsec/pkg/stores/ssmstore"
"go.jetpack.io/pkg/auth/session"
"go.jetpack.io/pkg/envvar"
"go.jetpack.io/pkg/filecache"
Expand Down Expand Up @@ -126,9 +126,9 @@ func GenSSMConfigFromToken(
ctx context.Context,
tok *session.Token,
useCache bool,
) (*envsec.SSMConfig, error) {
) (*ssmstore.SSMConfig, error) {
if tok == nil {
return &envsec.SSMConfig{}, nil
return &ssmstore.SSMConfig{}, nil
}
fed := New()
var creds *types.Credentials
Expand All @@ -141,7 +141,7 @@ func GenSSMConfigFromToken(
if err != nil {
return nil, errors.WithStack(err)
}
return &envsec.SSMConfig{
return &ssmstore.SSMConfig{
AccessKeyID: *creds.AccessKeyId,
SecretAccessKey: *creds.SecretKey,
SessionToken: *creds.SessionToken,
Expand Down
2 changes: 1 addition & 1 deletion envsec/pkg/envcli/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/envsec"
"go.jetpack.io/envsec/pkg/envsec"
)

type execCmdFlags struct {
Expand Down
28 changes: 15 additions & 13 deletions envsec/pkg/envcli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/envsec"
"go.jetpack.io/envsec/internal/build"
"go.jetpack.io/envsec/pkg/awsfed"
envsecLib "go.jetpack.io/envsec/pkg/envsec"
"go.jetpack.io/envsec/pkg/envsec"
"go.jetpack.io/envsec/pkg/stores/jetstore"
"go.jetpack.io/envsec/pkg/stores/ssmstore"
"go.jetpack.io/pkg/auth/session"
"go.jetpack.io/pkg/envvar"
"go.jetpack.io/pkg/id"
Expand Down Expand Up @@ -57,7 +58,7 @@ func (f *configFlags) validateProjectID(orgID id.OrgID) (string, error) {
if err != nil {
return "", errors.WithStack(err)
}
config, err := (&envsecLib.Envsec{
config, err := (&envsec.Envsec{
WorkingDir: wd,
IsDev: build.IsDev,
}).ProjectConfig()
Expand Down Expand Up @@ -95,20 +96,22 @@ func (f *configFlags) genConfig(cmd *cobra.Command) (*CmdConfig, error) {
var tok *session.Token
var err error

wd, err := os.Getwd()
if err != nil {
return nil, errors.WithStack(err)
}
envsecInstance := defaultEnvsec(cmd, wd)

if f.orgID == "" {
client, err := newAuthClient()
if err != nil {
return nil, err
}

wd, err := os.Getwd()
if err != nil {
return nil, errors.WithStack(err)
}
// This is a bit of a temporary hack. Ideally get,set,list logic moves
// into envsec lib and it will choose correct credentials based on org.
// It will also take flags that override project and organization.
project, _ := defaultEnvsec(cmd, wd).ProjectConfig()
project, _ := envsecInstance.ProjectConfig()

if project != nil {
tok, err = client.LoginFlowIfNeededForOrg(ctx, project.OrgID.String())
Expand All @@ -130,17 +133,16 @@ func (f *configFlags) genConfig(cmd *cobra.Command) (*CmdConfig, error) {
if err != nil {
return nil, errors.WithStack(err)
}
store, err = envsec.NewStore(ctx, ssmConfig)
store, err = ssmstore.New(ctx, ssmConfig)
if err != nil {
return nil, errors.WithStack(err)
}
} else {
store, err = envsec.NewStore(ctx, envsec.NewJetpackAPIConfig(tok))
if err != nil {
return nil, errors.WithStack(err)
}
store = &jetstore.JetpackAPIStore{}
}

store.Identify(ctx, envsecInstance, tok)

if err != nil {
return nil, errors.WithStack(err)
}
Expand Down
36 changes: 22 additions & 14 deletions envsec/pkg/envcli/ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@
package envcli

import (
"github.com/pkg/errors"
"os"

"github.com/spf13/cobra"
"go.jetpack.io/envsec"
"go.jetpack.io/envsec/pkg/envsec"
)

const environmentFlagName = "environment"
Expand All @@ -32,24 +33,31 @@ func ListCmd() *cobra.Command {
return err
}

// TODO: parallelize
envIDs := []envsec.EnvID{}
for _, envName := range cmdCfg.EnvNames {
envID := envsec.EnvID{
envIDs = append(envIDs, envsec.EnvID{
OrgID: cmdCfg.EnvID.OrgID,
ProjectID: cmdCfg.EnvID.ProjectID,
EnvName: envName,
}
envVars, err := cmdCfg.Store.List(cmd.Context(), envID)
if err != nil {
return errors.WithStack(err)
}
})
}

err = printEnv(cmd, envID, envVars, flags.ShowValues, flags.Format)
if err != nil {
return errors.WithStack(err)
}
wd, err := os.Getwd()
if err != nil {
return err
}
return nil

vars, err := defaultEnvsec(cmd, wd).List(
cmd.Context(),
cmdCfg.Store,
envIDs...,
)
if err != nil {
return err
}

return envsec.PrintEnvVars(
vars, cmd.OutOrStdout(), flags.ShowValues, flags.Format)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated to this PR:

I just noticed that the help message for --format isn't completely helpful. It could specify what the valid options are Display the key values in key=value format. Must be one of: table | dotenv | json".

},
}

Expand Down
4 changes: 2 additions & 2 deletions envsec/pkg/envcli/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

"github.com/pkg/errors"
"github.com/spf13/cobra"
"go.jetpack.io/envsec"
"go.jetpack.io/envsec/internal/tux"
"go.jetpack.io/envsec/pkg/stores/ssmstore"
)

type removeCmdFlags struct {
Expand Down Expand Up @@ -37,7 +37,7 @@ func RemoveCmd() *cobra.Command {
strings.ToLower(cmdCfg.EnvID.EnvName),
)
}
if errors.Is(err, envsec.FaultyParamError) {
if errors.Is(err, ssmstore.FaultyParamError) {
err = tux.WriteHeader(cmd.OutOrStdout(),
"[CANCELLED] Could not delete variable '%v' in environment: %s.\n"+
"Please make sure all listed variables exist and you have proper permission to remove them.\n",
Expand Down
Loading