-
Notifications
You must be signed in to change notification settings - Fork 157
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
Closed
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
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(), | ||
), | ||
Uses: []schema.CredentialUsage{ | ||
{ | ||
Name: credname.UserLogin, | ||
}, | ||
}, | ||
} | ||
} |
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,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(), | ||
}, | ||
} | ||
} |
Empty file.
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,85 @@ | ||
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"), // TODO: Replace with actual URL | ||
ManagementURL: sdk.URL("https://console.docker.com/user/security/tokens"), // TODO: Replace with actual URL | ||
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.EnvVars(defaultEnvVarMapping), | ||
Importer: importer.TryAll( | ||
|
||
TryDockerConfigFile(), | ||
)} | ||
} | ||
|
||
var defaultEnvVarMapping = map[string]sdk.FieldName{ | ||
"DOCKER_USERNAME": fieldname.Username, | ||
"DOCKER_PASSWORD": fieldname.Password, | ||
} | ||
|
||
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{ | ||
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 | ||
} |
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,59 @@ | ||
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: "4LjPnJ2u4Yo02KRfP7ffF1Tf2eoDZxnvNEXAMPLE", | ||
fieldname.Password: "12345", | ||
}, | ||
ExpectedOutput: sdk.ProvisionOutput{ | ||
Environment: map[string]string{ | ||
"DOCKER_USERNAME": "4LjPnJ2u4Yo02KRfP7ffF1Tf2eoDZxnvNEXAMPLE", | ||
"DOCKER_PASSWORD": "12345", | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestUserLoginImporter(t *testing.T) { | ||
plugintest.TestImporter(t, UserLogin().Importer, map[string]plugintest.ImportCase{ | ||
"environment": { | ||
Environment: map[string]string{ | ||
"DOCKER_USERNAME": "4LjPnJ2u4Yo02KRfP7ffF1Tf2eoDZxnvNEXAMPLE", | ||
"DOCKER_PASSWORD": "12345", | ||
}, | ||
ExpectedCandidates: []sdk.ImportCandidate{ | ||
{ | ||
Fields: map[sdk.FieldName]string{ | ||
fieldname.Username: "4LjPnJ2u4Yo02KRfP7ffF1Tf2eoDZxnvNEXAMPLE", | ||
fieldname.Password: "12345", | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
"config file": { | ||
Files: map[string]string{ | ||
"~/.docker/config.json": plugintest.LoadFixture(t, "config.json"), | ||
}, | ||
ExpectedCandidates: []sdk.ImportCandidate{ | ||
{ | ||
Fields: map[sdk.FieldName]string{ | ||
fieldname.Username: "4LjPnJ2u4Yo02KRfP7ffF1Tf2eoDZxnvNEXAMPLE", | ||
fieldname.Password: "12345", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not able to find anywhere documentation related to this authentication method for docker against DockerHub (or other platforms).
Can you please provide some documentation on this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yess here it is https://docs.docker.com/engine/reference/commandline/login/