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

Add support for docker cli login using Username and Email plugin #301

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 26 additions & 0 deletions plugins/docker/docker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package docker

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 DockerCLI() schema.Executable {
return schema.Executable{
Name: "Docker CLI",
Runs: []string{"docker"},
DocsURL: sdk.URL("https://docker.com/docs/cli"),
NeedsAuth: needsauth.IfAll(
needsauth.NotForHelpOrVersion(),
needsauth.NotWithoutArgs(),
needsauth.ForCommand("login", "push", "pull"),
),
Uses: []schema.CredentialUsage{
{
Name: credname.UserLogin,
},
},
}
}
22 changes: 22 additions & 0 deletions plugins/docker/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package docker

import (
"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/schema"
)

func New() schema.Plugin {
return schema.Plugin{
Name: "docker",
Platform: schema.PlatformInfo{
Name: "Docker",
Homepage: sdk.URL("https://docker.com"),
},
Credentials: []schema.CredentialType{
UserLogin(),
},
Executables: []schema.Executable{
DockerCLI(),
},
}
}
7 changes: 7 additions & 0 deletions plugins/docker/test-fixtures/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"auths":{
"https://index.docker.io/v1/":{
"auth":"U3RyZWFtOlN0cmVhbUAwODc="
}
}
}
96 changes: 96 additions & 0 deletions plugins/docker/user_login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package docker

import (
"context"
"encoding/base64"
"strings"

"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 UserLogin() schema.CredentialType {
return schema.CredentialType{
Name: credname.UserLogin,
DocsURL: sdk.URL("https://docker.com/docs/user_login"),
ManagementURL: sdk.URL("https://console.docker.com/user/security/tokens"),
Fields: []schema.CredentialField{
{
Name: fieldname.Username,
MarkdownDescription: "Username of the user registered in Docker.",
},
{
Name: fieldname.Password,
MarkdownDescription: "Password of the user registered in Docker.",
Secret: true,
},
},
DefaultProvisioner: provision.TempFile(dockerConfig, provision.AtFixedPath("~/.docker/config.json")),
Importer: importer.TryAll(TryDockerConfigFile()),
}
}

func dockerConfig(in sdk.ProvisionInput) ([]byte, error) {
content := "{\n"
content += "\"auths\":{\n"

if username, ok := in.ItemFields[fieldname.Username]; ok {
auth := base64.StdEncoding.EncodeToString([]byte(username + ":" + in.ItemFields[fieldname.Password]))
content += "\"https://index.docker.io/v1/\":{\n"
content += "\"auth\":\"" + auth + "\"\n"
content += "}\n"
}

content += "}\n"
content += "}\n"

return []byte(content), nil
}

func TryDockerConfigFile() sdk.Importer {
return importer.TryFile("~/.docker/config.json", func(ctx context.Context, contents importer.FileContents, in sdk.ImportInput, out *sdk.ImportAttempt) {

var config struct {
Auths map[string]struct {
Auth string `json:"auth"`
} `json:"auths"`
}

if err := contents.ToJSON(&config); err != nil {
out.AddError(err)
return
}
for _, auth := range config.Auths {
decoded, err := base64.StdEncoding.DecodeString(auth.Auth)
if err != nil {
out.AddError(err)
return
}
credentials := string(decoded)
username, password := parseCredentials(credentials)
if username != "" {
out.AddCandidate(sdk.ImportCandidate{
NameHint: "",
Fields: map[sdk.FieldName]string{
fieldname.Username: username,
fieldname.Password: password,
},
})
}
}

})
}

func parseCredentials(credentials string) (username string, password string) {
parts := strings.SplitN(credentials, ":", 2)
if len(parts) == 2 {
username = parts[0]
password = parts[1]
}
return username, password
}
45 changes: 45 additions & 0 deletions plugins/docker/user_login_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package docker

import (
"testing"

"github.com/1Password/shell-plugins/sdk"
"github.com/1Password/shell-plugins/sdk/plugintest"
"github.com/1Password/shell-plugins/sdk/schema/fieldname"
)

func TestUserLoginProvisioner(t *testing.T) {
plugintest.TestProvisioner(t, UserLogin().DefaultProvisioner, map[string]plugintest.ProvisionCase{
"default": {
ItemFields: map[sdk.FieldName]string{
fieldname.Username: "Stream",
fieldname.Password: "Stream@087",
// fieldname.URL: "https://index.docker.io/v1/",
},
ExpectedOutput: sdk.ProvisionOutput{

Files: map[string]sdk.OutputFile{
"~/.docker/config.json": {Contents: []byte(plugintest.LoadFixture(t, "config.json"))},
},
},
},
})
}

func TestUserLoginImporter(t *testing.T) {
plugintest.TestImporter(t, UserLogin().Importer, map[string]plugintest.ImportCase{
"config file": {
Files: map[string]string{
"~/.docker/config.json": plugintest.LoadFixture(t, "config.json"),
},
ExpectedCandidates: []sdk.ImportCandidate{
{
Fields: map[sdk.FieldName]string{
fieldname.Username: "Stream",
fieldname.Password: "Stream@087",
},
},
},
},
})
}