forked from notaryproject/notation
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Feature] support notation login (notaryproject#218)
* feat: support notation login * refactor: Refactor for readablity * chore: refactor registry.go * feat: update CMDs to be consistent with specs * chore: refactor load config logic Signed-off-by: Binbin Li <[email protected]>
- Loading branch information
Showing
19 changed files
with
433 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/notaryproject/notation/pkg/auth" | ||
"github.com/urfave/cli/v2" | ||
orasauth "oras.land/oras-go/v2/registry/remote/auth" | ||
) | ||
|
||
var loginCommand = &cli.Command{ | ||
Name: "login", | ||
Usage: "Provides credentials for authenticated registry operations", | ||
UsageText: `notation login [options] [server] | ||
Example - Login with provided username and password: | ||
notation login -u <user> -p <password> registry.example.com | ||
Example - Login using $NOTATION_USERNAME $NOTATION_PASSWORD variables: | ||
notation login registry.example.com`, | ||
ArgsUsage: "[server]", | ||
Flags: []cli.Flag{ | ||
flagUsername, | ||
flagPassword, | ||
&cli.BoolFlag{ | ||
Name: "password-stdin", | ||
Usage: "Take the password from stdin", | ||
}, | ||
}, | ||
Before: readPassword, | ||
Action: runLogin, | ||
} | ||
|
||
func runLogin(ctx *cli.Context) error { | ||
// initialize | ||
if !ctx.Args().Present() { | ||
return errors.New("no hostname specified") | ||
} | ||
serverAddress := ctx.Args().First() | ||
|
||
if err := validateAuthConfig(ctx, serverAddress); err != nil { | ||
return err | ||
} | ||
|
||
nativeStore, err := auth.GetCredentialsStore(serverAddress) | ||
if err != nil { | ||
return fmt.Errorf("could not get the credentials store: %v", err) | ||
} | ||
// init creds | ||
creds := newCredentialFromInput( | ||
ctx.String(flagUsername.Name), | ||
ctx.String(flagPassword.Name), | ||
) | ||
if err = nativeStore.Store(serverAddress, creds); err != nil { | ||
return fmt.Errorf("failed to store credentials: %v", err) | ||
} | ||
return nil | ||
} | ||
|
||
func validateAuthConfig(ctx *cli.Context, serverAddress string) error { | ||
registry, err := getRegistryClient(ctx, serverAddress) | ||
if err != nil { | ||
return err | ||
} | ||
return registry.Ping(ctx.Context) | ||
} | ||
|
||
func newCredentialFromInput(username, password string) orasauth.Credential { | ||
c := orasauth.Credential{ | ||
Username: username, | ||
Password: password, | ||
} | ||
if c.Username == "" { | ||
c.RefreshToken = password | ||
} | ||
return c | ||
} | ||
|
||
func readPassword(ctx *cli.Context) error { | ||
if ctx.Bool("password-stdin") { | ||
password, err := readLine() | ||
if err != nil { | ||
return err | ||
} | ||
ctx.Set(flagPassword.Name, password) | ||
} | ||
return nil | ||
} | ||
|
||
func readLine() (string, error) { | ||
passwordBytes, err := io.ReadAll(os.Stdin) | ||
if err != nil { | ||
return "", err | ||
} | ||
password := strings.TrimSuffix(string(passwordBytes), "\n") | ||
password = strings.TrimSuffix(password, "\r") | ||
return password, nil | ||
} |
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,28 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/notaryproject/notation/pkg/auth" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
var logoutCommand = &cli.Command{ | ||
Name: "logout", | ||
Usage: "Log out the specified registry hostname", | ||
ArgsUsage: "[server]", | ||
Action: runLogout, | ||
} | ||
|
||
func runLogout(ctx *cli.Context) error { | ||
// initialize | ||
if !ctx.Args().Present() { | ||
return errors.New("no hostname specified") | ||
} | ||
serverAddress := ctx.Args().First() | ||
nativeStore, err := auth.GetCredentialsStore(serverAddress) | ||
if err != nil { | ||
return err | ||
} | ||
return nativeStore.Erase(serverAddress) | ||
} |
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
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,13 @@ | ||
package auth | ||
|
||
import "oras.land/oras-go/v2/registry/remote/auth" | ||
|
||
// CredentialStore is the interface that any credentials store must implement. | ||
type CredentialStore interface { | ||
// Store saves credentials into the store | ||
Store(serverAddress string, credsConf auth.Credential) error | ||
// Erase removes credentials from the store for the given server | ||
Erase(serverAddress string) error | ||
// Get retrieves credentials from the store for the given server | ||
Get(serverAddress string) (auth.Credential, error) | ||
} |
Oops, something went wrong.