-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
452 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/influxdata/influxdb" | ||
"github.com/influxdata/influxdb/http" | ||
"github.com/spf13/cobra" | ||
input "github.com/tcnksm/go-input" | ||
) | ||
|
||
type secretSVCsFn func() (influxdb.SecretService, influxdb.OrganizationService, func(*input.UI) string, error) | ||
|
||
func cmdSecret(opts ...genericCLIOptFn) *cobra.Command { | ||
return newCmdSecretBuilder(newSecretSVCs, opts...).cmd() | ||
} | ||
|
||
type cmdSecretBuilder struct { | ||
genericCLIOpts | ||
|
||
svcFn secretSVCsFn | ||
|
||
key string | ||
org organization | ||
} | ||
|
||
func newCmdSecretBuilder(svcsFn secretSVCsFn, opts ...genericCLIOptFn) *cmdSecretBuilder { | ||
opt := genericCLIOpts{ | ||
in: os.Stdin, | ||
w: os.Stdout, | ||
} | ||
for _, o := range opts { | ||
o(&opt) | ||
} | ||
|
||
return &cmdSecretBuilder{ | ||
genericCLIOpts: opt, | ||
svcFn: svcsFn, | ||
} | ||
} | ||
|
||
func (b *cmdSecretBuilder) cmd() *cobra.Command { | ||
cmd := b.newCmd("secret", nil) | ||
cmd.Short = "Secret management commands" | ||
cmd.Run = seeHelp | ||
cmd.AddCommand( | ||
b.cmdDelete(), | ||
b.cmdFind(), | ||
b.cmdUpdate(), | ||
) | ||
return cmd | ||
} | ||
|
||
func (b *cmdSecretBuilder) cmdUpdate() *cobra.Command { | ||
cmd := b.newCmd("update", b.cmdUpdateRunEFn) | ||
cmd.Short = "Update secret" | ||
cmd.Flags().StringVarP(&b.key, "key", "k", "", "The secret key (required)") | ||
cmd.MarkFlagRequired("key") | ||
b.org.register(cmd, false) | ||
|
||
return cmd | ||
} | ||
|
||
func (b *cmdSecretBuilder) cmdDelete() *cobra.Command { | ||
cmd := b.newCmd("delete", b.cmdDeleteRunEFn) | ||
cmd.Short = "Delete secret" | ||
|
||
cmd.Flags().StringVarP(&b.key, "key", "k", "", "The secret key (required)") | ||
cmd.MarkFlagRequired("key") | ||
b.org.register(cmd, false) | ||
|
||
return cmd | ||
} | ||
|
||
func (b *cmdSecretBuilder) cmdUpdateRunEFn(cmd *cobra.Command, args []string) error { | ||
scrSVC, orgSVC, getSecretFn, err := b.svcFn() | ||
if err != nil { | ||
return err | ||
} | ||
orgID, err := b.org.getID(orgSVC) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
ui := &input.UI{ | ||
Writer: b.genericCLIOpts.w, | ||
Reader: b.genericCLIOpts.in, | ||
} | ||
secret := getSecretFn(ui) | ||
|
||
if err := scrSVC.PatchSecrets(ctx, orgID, map[string]string{ | ||
b.key: secret, | ||
}); err != nil { | ||
return fmt.Errorf("failed to update secret with key %q: %v", b.key, err) | ||
} | ||
|
||
w := b.newTabWriter() | ||
w.WriteHeaders("Key", "OrgID", "Updated") | ||
w.Write(map[string]interface{}{ | ||
"Key": b.key, | ||
"OrgID": orgID, | ||
"Updated": true, | ||
}) | ||
w.Flush() | ||
|
||
return nil | ||
} | ||
|
||
func (b *cmdSecretBuilder) cmdDeleteRunEFn(cmd *cobra.Command, args []string) error { | ||
scrSVC, orgSVC, _, err := b.svcFn() | ||
if err != nil { | ||
return err | ||
} | ||
orgID, err := b.org.getID(orgSVC) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ctx := context.Background() | ||
if err := scrSVC.DeleteSecret(ctx, orgID, b.key); err != nil { | ||
return fmt.Errorf("failed to delete secret with key %q: %v", b.key, err) | ||
} | ||
|
||
w := b.newTabWriter() | ||
w.WriteHeaders("Key", "OrgID", "Deleted") | ||
w.Write(map[string]interface{}{ | ||
"Key": b.key, | ||
"OrgID": orgID, | ||
"Deleted": true, | ||
}) | ||
w.Flush() | ||
|
||
return nil | ||
} | ||
|
||
func (b *cmdSecretBuilder) cmdFind() *cobra.Command { | ||
cmd := b.newCmd("find", b.cmdFindRunEFn) | ||
cmd.Short = "Find secrets" | ||
b.org.register(cmd, false) | ||
|
||
return cmd | ||
} | ||
|
||
func (b *cmdSecretBuilder) cmdFindRunEFn(cmd *cobra.Command, args []string) error { | ||
|
||
scrSVC, orgSVC, _, err := b.svcFn() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
orgID, err := b.org.getID(orgSVC) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
secrets, err := scrSVC.GetSecretKeys(context.Background(), orgID) | ||
if err != nil { | ||
return fmt.Errorf("failed to retrieve secret keys: %s", err) | ||
} | ||
|
||
w := b.newTabWriter() | ||
w.WriteHeaders("Key", "OrganizationID") | ||
for _, s := range secrets { | ||
w.Write(map[string]interface{}{ | ||
"Key": s, | ||
"OrganizationID": orgID, | ||
}) | ||
} | ||
w.Flush() | ||
|
||
return nil | ||
} | ||
|
||
func newSecretSVCs() (influxdb.SecretService, influxdb.OrganizationService, func(*input.UI) string, error) { | ||
httpClient, err := newHTTPClient() | ||
if err != nil { | ||
return nil, nil, nil, err | ||
} | ||
orgSvc := &http.OrganizationService{Client: httpClient} | ||
|
||
return &http.SecretService{Client: httpClient}, orgSvc, getSecret, nil | ||
} |
Oops, something went wrong.