Skip to content

Commit

Permalink
update keystore service name to use app bundle name
Browse files Browse the repository at this point in the history
  • Loading branch information
tlowerison committed Apr 30, 2021
1 parent 71387c9 commit 9673488
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import (
)

func main() {
serviceName := "credential-1password"
ctx := util.NewContext(serviceName, op.Op, keystore.NewKeystore(serviceName), os.Stdin)
ctx := util.NewContext(op.Op, keystore.NewKeystore(util.ServiceName), os.Stdin)

var rootCmd *cobra.Command
rootCmd = &cobra.Command{
Expand Down
8 changes: 4 additions & 4 deletions util/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ const ErrMsgUnknownCommand = "unable to read inputs in the correct format withou
const ErrMsgDockerServerUrlBadInputZeroLines = "cannot parse url from zero lines of input"
const ErrMsgDockerServerUrlBadInputMultipleLines = "cannot parse url from multiple lines of input"
const ErrMsgClosedStdinAfterDeadline = "closed stdin after waiting"
const ServiceName = "com.tlowerison.credential-1password"

const sessionTokenDateKey = "session-token.date"
const sessionTokenValueKey = "session-token.value"
Expand All @@ -83,14 +84,13 @@ const dockerServerURLKey = "ServerURL"
const timeFormat = time.UnixDate
const defaultStdinDeadline = 30 * time.Second

func NewContext(serviceName string, opFunc op.OpFunc, ks keystore.Keystore, stdin io.ReadCloser) *Context {
func NewContext(opFunc op.OpFunc, ks keystore.Keystore, stdin io.ReadCloser) *Context {
return &Context{
Flags: &Flags{},
OpFunc: opFunc,
opCtx: &op.Context{},
inputs: map[string]string{},
keystore: ks,
serviceName: serviceName,
stdin: stdin,
stdinDeadline: defaultStdinDeadline,
}
Expand Down Expand Up @@ -166,9 +166,9 @@ func (ctx *Context) GetMode() Mode {
func (ctx *Context) GetName() string {
mode := ctx.GetMode()
if mode.IsPredefined() {
return fmt.Sprintf("%s-%s", string(mode), ctx.serviceName)
return fmt.Sprintf("%s-%s", string(mode), "credential-1password")
}
return ctx.serviceName
return "credential-1password"
}

// GetOpQuery wraps an op.Context and the input key provided over
Expand Down
40 changes: 20 additions & 20 deletions util/test/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ func TestModeValid(t *testing.T) {
}

func TestNewContext(t *testing.T) {
ctx := util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(""))
ctx := util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(""))
require.NotNil(t, ctx)
}

func TestContextCmd(t *testing.T) {
ctx := util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(""))
ctx := util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(""))
expCmd := &cobra.Command{}

ctx.SetCmd(expCmd)
Expand All @@ -83,7 +83,7 @@ func TestContextCmd(t *testing.T) {

func TestContextInputGet(t *testing.T) {
input := ""
ctx := util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx := util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)

err := ctx.ParseInput()
Expand All @@ -92,7 +92,7 @@ func TestContextInputGet(t *testing.T) {

// timeout (Fail)
stdin := newTestStdin("")
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), stdin)
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), stdin)
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "get"})
ctx.SetStdinDeadline(50 * time.Millisecond)
Expand All @@ -105,7 +105,7 @@ func TestContextInputGet(t *testing.T) {

// timeout (OK)
stdin = newTestStdin("")
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), stdin)
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), stdin)
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "get"})
ctx.SetStdinDeadline(100 * time.Millisecond)
Expand All @@ -117,7 +117,7 @@ func TestContextInputGet(t *testing.T) {

// non-predefined mode get
input = ""
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "get"})

Expand All @@ -129,7 +129,7 @@ func TestContextInputGet(t *testing.T) {
require.Equal(t, map[string]string{}, inputs)

// posititional arguments shouldn't obfuscate command name
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "get <foo> <bar>"})

Expand All @@ -142,7 +142,7 @@ func TestContextInputGet(t *testing.T) {

// happy path git-credential-1password get
input = "protocol=https\nhost=github.com"
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "get"})
ctx.Flags.Mode = string(util.GitMode)
Expand All @@ -159,7 +159,7 @@ func TestContextInputGet(t *testing.T) {

// empty git-credential-1password get (OK)
input = ""
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "get"})
ctx.Flags.Mode = string(util.GitMode)
Expand All @@ -169,7 +169,7 @@ func TestContextInputGet(t *testing.T) {

// happy path docker-credential-1password get
input = "https://index.docker.io/v1/"
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "get"})
ctx.Flags.Mode = string(util.DockerMode)
Expand All @@ -185,7 +185,7 @@ func TestContextInputGet(t *testing.T) {

// empty docker-credential-1password get
input = ""
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "get"})
ctx.Flags.Mode = string(util.DockerMode)
Expand All @@ -196,7 +196,7 @@ func TestContextInputGet(t *testing.T) {

// multiple lines docker-credential-1password get
input = "https://index.docker.io/v1/\nhttps://index.docker.io/v1/"
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "get"})
ctx.Flags.Mode = string(util.DockerMode)
Expand All @@ -208,15 +208,15 @@ func TestContextInputGet(t *testing.T) {

func TestContextInputStore(t *testing.T) {
input := ""
ctx := util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx := util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)

err := ctx.ParseInput()
require.NotNil(t, err)
require.Equal(t, util.ErrMsgUnknownCommand, err.Error())

// timeout
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), os.Stdin)
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), os.Stdin)
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "store"})
ctx.SetStdinDeadline(0 * time.Second)
Expand All @@ -227,7 +227,7 @@ func TestContextInputStore(t *testing.T) {

// non-predefined mode store
input = "@scope:registry=https://registry.yarnpkg.com/\n_authToken=my-auth-token\nalways-auth=true"
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "store"})

Expand All @@ -240,7 +240,7 @@ func TestContextInputStore(t *testing.T) {

// posititional arguments shouldn't obfuscate command name
input = "@scope:registry=https://registry.yarnpkg.com/\n_authToken=my-auth-token\nalways-auth=true"
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "store <foo> <bar>"})

Expand All @@ -253,7 +253,7 @@ func TestContextInputStore(t *testing.T) {

// happy path git-credential-1password store
input = "protocol=https\nhost=github.com\nusername=my-username\npassword=my-password"
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "store"})
ctx.Flags.Mode = string(util.GitMode)
Expand All @@ -272,7 +272,7 @@ func TestContextInputStore(t *testing.T) {

// happy path docker-credential-1password store
input = "{\"ServerURL\": \"https://index.docker.io/v1/\",\n \"Username\": \"my-username\",\n \"Secret\": \"my-secret\" }"
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "store"})
ctx.Flags.Mode = string(util.DockerMode)
Expand All @@ -290,7 +290,7 @@ func TestContextInputStore(t *testing.T) {

// empty docker-credential-1password store
input = ""
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "store"})
ctx.Flags.Mode = string(util.DockerMode)
Expand All @@ -301,7 +301,7 @@ func TestContextInputStore(t *testing.T) {

// only url docker-credential-1password store
input = "https://index.docker.io/v1/"
ctx = util.NewContext(serviceName, testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
ctx = util.NewContext(testOpFunc, keystore.NewMockKeystore(nil, nil), newTestStdin(input))
require.NotNil(t, ctx)
ctx.SetCmd(&cobra.Command{Use: "store"})
ctx.Flags.Mode = string(util.DockerMode)
Expand Down

0 comments on commit 9673488

Please sign in to comment.