From 7ba215a5b7ad2eb6c38f3e465baea2c1508b51e1 Mon Sep 17 00:00:00 2001 From: Maniraja Date: Sat, 1 Jul 2023 01:35:39 -0400 Subject: [PATCH 1/4] Add support for Kaggle CLI --- plugins/kaggle/api_token.go | 51 ++++++++++++++++++++++++++++++++ plugins/kaggle/api_token_test.go | 26 ++++++++++++++++ plugins/kaggle/kaggle.go | 25 ++++++++++++++++ plugins/kaggle/plugin.go | 22 ++++++++++++++ 4 files changed, 124 insertions(+) create mode 100644 plugins/kaggle/api_token.go create mode 100644 plugins/kaggle/api_token_test.go create mode 100644 plugins/kaggle/kaggle.go create mode 100644 plugins/kaggle/plugin.go diff --git a/plugins/kaggle/api_token.go b/plugins/kaggle/api_token.go new file mode 100644 index 000000000..5b047a6e9 --- /dev/null +++ b/plugins/kaggle/api_token.go @@ -0,0 +1,51 @@ +package kaggle + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/importer" + "github.com/1Password/shell-plugins/sdk/provision" + "github.com/1Password/shell-plugins/sdk/schema" + "github.com/1Password/shell-plugins/sdk/schema/credname" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func APIToken() schema.CredentialType { + return schema.CredentialType{ + Name: credname.APIToken, + DocsURL: sdk.URL("https://www.kaggle.com/docs/api"), + ManagementURL: sdk.URL("https://www.kaggle.com/settings/account"), + Fields: []schema.CredentialField{ + { + Name: fieldname.Token, + MarkdownDescription: "API Token used to authenticate to Kaggle.", + Secret: true, + Composition: &schema.ValueComposition{ + Length: 32, + Charset: schema.Charset{ + Lowercase: true, + Digits: true, + }, + }, + }, + { + Name: fieldname.Username, + MarkdownDescription: "Username to authenticate to Kaggle.", + Secret: true, + Composition: &schema.ValueComposition{ + Charset: schema.Charset{ + Lowercase: true, + Digits: true, + }, + }, + }, + }, + DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), + Importer: importer.TryAll( + importer.TryEnvVarPair(defaultEnvVarMapping), + )} +} + +var defaultEnvVarMapping = map[string]sdk.FieldName{ + "KAGGLE_TOKEN": fieldname.Token, + "KAGGLE_USERNAME": fieldname.Username, +} diff --git a/plugins/kaggle/api_token_test.go b/plugins/kaggle/api_token_test.go new file mode 100644 index 000000000..39f7d2861 --- /dev/null +++ b/plugins/kaggle/api_token_test.go @@ -0,0 +1,26 @@ +package kaggle + +import ( + "testing" + + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/plugintest" + "github.com/1Password/shell-plugins/sdk/schema/fieldname" +) + +func TestAPITokenProvisioner(t *testing.T) { + plugintest.TestProvisioner(t, APIToken().DefaultProvisioner, map[string]plugintest.ProvisionCase{ + "default": { + ItemFields: map[sdk.FieldName]string{ + fieldname.Token: "z2pifkruzgbb17plmz2gux21fexample", + fieldname.Username: "username", + }, + ExpectedOutput: sdk.ProvisionOutput{ + Environment: map[string]string{ + "KAGGLE_TOKEN": "z2pifkruzgbb17plmz2gux21fexample", + "KAGGLE_USERNAME": "username", + }, + }, + }, + }) +} diff --git a/plugins/kaggle/kaggle.go b/plugins/kaggle/kaggle.go new file mode 100644 index 000000000..78d0dd2a2 --- /dev/null +++ b/plugins/kaggle/kaggle.go @@ -0,0 +1,25 @@ +package kaggle + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/needsauth" + "github.com/1Password/shell-plugins/sdk/schema" + "github.com/1Password/shell-plugins/sdk/schema/credname" +) + +func KaggleCLI() schema.Executable { + return schema.Executable{ + Name: "Kaggle CLI", + Runs: []string{"kaggle"}, + DocsURL: sdk.URL("https://github.com/Kaggle/kaggle-api"), + NeedsAuth: needsauth.IfAll( + needsauth.NotForHelpOrVersion(), + needsauth.NotWithoutArgs(), + ), + Uses: []schema.CredentialUsage{ + { + Name: credname.APIToken, + }, + }, + } +} diff --git a/plugins/kaggle/plugin.go b/plugins/kaggle/plugin.go new file mode 100644 index 000000000..2a6b256cd --- /dev/null +++ b/plugins/kaggle/plugin.go @@ -0,0 +1,22 @@ +package kaggle + +import ( + "github.com/1Password/shell-plugins/sdk" + "github.com/1Password/shell-plugins/sdk/schema" +) + +func New() schema.Plugin { + return schema.Plugin{ + Name: "kaggle", + Platform: schema.PlatformInfo{ + Name: "Kaggle", + Homepage: sdk.URL("https://kaggle.com"), + }, + Credentials: []schema.CredentialType{ + APIToken(), + }, + Executables: []schema.Executable{ + KaggleCLI(), + }, + } +} From 1e9608f21638ff84615c9f380b42a62c0784b752 Mon Sep 17 00:00:00 2001 From: Maniraja Date: Sat, 1 Jul 2023 01:37:05 -0400 Subject: [PATCH 2/4] Update incorrect environment variable for Kaggle key --- plugins/kaggle/api_token.go | 2 +- plugins/kaggle/api_token_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/kaggle/api_token.go b/plugins/kaggle/api_token.go index 5b047a6e9..81b6b0ad8 100644 --- a/plugins/kaggle/api_token.go +++ b/plugins/kaggle/api_token.go @@ -46,6 +46,6 @@ func APIToken() schema.CredentialType { } var defaultEnvVarMapping = map[string]sdk.FieldName{ - "KAGGLE_TOKEN": fieldname.Token, + "KAGGLE_KEY": fieldname.Token, "KAGGLE_USERNAME": fieldname.Username, } diff --git a/plugins/kaggle/api_token_test.go b/plugins/kaggle/api_token_test.go index 39f7d2861..89ff819e2 100644 --- a/plugins/kaggle/api_token_test.go +++ b/plugins/kaggle/api_token_test.go @@ -17,7 +17,7 @@ func TestAPITokenProvisioner(t *testing.T) { }, ExpectedOutput: sdk.ProvisionOutput{ Environment: map[string]string{ - "KAGGLE_TOKEN": "z2pifkruzgbb17plmz2gux21fexample", + "KAGGLE_KEY": "z2pifkruzgbb17plmz2gux21fexample", "KAGGLE_USERNAME": "username", }, }, From 91e933dff6bbdf957604c79c9e48cef111a60c45 Mon Sep 17 00:00:00 2001 From: Maniraja Date: Mon, 17 Jul 2023 21:54:50 -0400 Subject: [PATCH 3/4] Address feedback --- plugins/kaggle/api_token.go | 30 +++++++++++++++++++++ plugins/kaggle/api_token_test.go | 33 ++++++++++++++++++++++++ plugins/kaggle/test-fixtures/config.json | 1 + 3 files changed, 64 insertions(+) create mode 100644 plugins/kaggle/test-fixtures/config.json diff --git a/plugins/kaggle/api_token.go b/plugins/kaggle/api_token.go index 81b6b0ad8..4c519e8d5 100644 --- a/plugins/kaggle/api_token.go +++ b/plugins/kaggle/api_token.go @@ -1,6 +1,8 @@ package kaggle import ( + "context" + "github.com/1Password/shell-plugins/sdk" "github.com/1Password/shell-plugins/sdk/importer" "github.com/1Password/shell-plugins/sdk/provision" @@ -42,6 +44,7 @@ func APIToken() schema.CredentialType { DefaultProvisioner: provision.EnvVars(defaultEnvVarMapping), Importer: importer.TryAll( importer.TryEnvVarPair(defaultEnvVarMapping), + TryKaggleConfigFile("~/.kaggle/kaggle.json"), )} } @@ -49,3 +52,30 @@ var defaultEnvVarMapping = map[string]sdk.FieldName{ "KAGGLE_KEY": fieldname.Token, "KAGGLE_USERNAME": fieldname.Username, } + +func TryKaggleConfigFile(path string) sdk.Importer { + return importer.TryFile(path, func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) { + var config Config + if err := contents.ToJSON(&config); err != nil { + out.AddError(err) + return + } + + if config.Token == "" { + return + } + + out.AddCandidate(sdk.ImportCandidate{ + Fields: map[sdk.FieldName]string{ + fieldname.Token: config.Token, + fieldname.Username: config.Username, + }, + NameHint: importer.SanitizeNameHint(config.Username), + }) + }) +} + +type Config struct { + Username string `json:"username"` + Token string `json:"key"` +} diff --git a/plugins/kaggle/api_token_test.go b/plugins/kaggle/api_token_test.go index 89ff819e2..d2f2fab05 100644 --- a/plugins/kaggle/api_token_test.go +++ b/plugins/kaggle/api_token_test.go @@ -24,3 +24,36 @@ func TestAPITokenProvisioner(t *testing.T) { }, }) } + +func TestAPITokenImporter(t *testing.T) { + plugintest.TestImporter(t, APIToken().Importer, map[string]plugintest.ImportCase{ + "environment": { + Environment: map[string]string{ + "KAGGLE_KEY": "z2pifkruzgbb17plmz2gux21fexample", + "KAGGLE_USERNAME": "username", + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "z2pifkruzgbb17plmz2gux21fexample", + fieldname.Username: "username", + }, + }, + }, + }, + "config file": { + Files: map[string]string{ + "~/.kaggle/kaggle.json": plugintest.LoadFixture(t, "config.json"), + }, + ExpectedCandidates: []sdk.ImportCandidate{ + { + Fields: map[sdk.FieldName]string{ + fieldname.Token: "z2pifkruzgbb17plmz2gux21fexample", + fieldname.Username: "username", + }, + NameHint: "username", + }, + }, + }, + }) +} diff --git a/plugins/kaggle/test-fixtures/config.json b/plugins/kaggle/test-fixtures/config.json new file mode 100644 index 000000000..943d14980 --- /dev/null +++ b/plugins/kaggle/test-fixtures/config.json @@ -0,0 +1 @@ +{"username":"username","key":"z2pifkruzgbb17plmz2gux21fexample"} \ No newline at end of file From 07f67105f0dc396a274b6e723194214b8cd37482 Mon Sep 17 00:00:00 2001 From: Arun Sathiya Date: Tue, 25 Jul 2023 07:37:16 -0700 Subject: [PATCH 4/4] Update username field as it is not a secret Co-authored-by: Simon Barendse --- plugins/kaggle/api_token.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/kaggle/api_token.go b/plugins/kaggle/api_token.go index 4c519e8d5..5553d14a7 100644 --- a/plugins/kaggle/api_token.go +++ b/plugins/kaggle/api_token.go @@ -32,7 +32,7 @@ func APIToken() schema.CredentialType { { Name: fieldname.Username, MarkdownDescription: "Username to authenticate to Kaggle.", - Secret: true, + Secret: false, Composition: &schema.ValueComposition{ Charset: schema.Charset{ Lowercase: true,