-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Shiwei Zhang <shizh@microsoft.com>
- Loading branch information
Showing
32 changed files
with
1,509 additions
and
201 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, build with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# VS Code | ||
.vscode | ||
debug | ||
|
||
# Jetbrains | ||
.idea |
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 |
---|---|---|
@@ -1,25 +1,36 @@ | ||
package main | ||
package common | ||
|
||
import "github.com/urfave/cli/v2" | ||
|
||
// Common flags | ||
var ( | ||
usernameFlag = &cli.StringFlag{ | ||
UsernameFlag = &cli.StringFlag{ | ||
Name: "username", | ||
Aliases: []string{"u"}, | ||
Usage: "username for generic remote access", | ||
} | ||
passwordFlag = &cli.StringFlag{ | ||
PasswordFlag = &cli.StringFlag{ | ||
Name: "password", | ||
Aliases: []string{"p"}, | ||
Usage: "password for generic remote access", | ||
} | ||
insecureFlag = &cli.BoolFlag{ | ||
InsecureFlag = &cli.BoolFlag{ | ||
Name: "insecure", | ||
Usage: "enable insecure remote access", | ||
} | ||
mediaTypeFlag = &cli.StringFlag{ | ||
MediaTypeFlag = &cli.StringFlag{ | ||
Name: "media-type", | ||
Usage: "specify the media type of the manifest read from file or stdin", | ||
Value: "application/vnd.docker.distribution.manifest.v2+json", | ||
} | ||
ExpiryFlag = &cli.DurationFlag{ | ||
Name: "expiry", | ||
Aliases: []string{"e"}, | ||
Usage: "expire duration", | ||
} | ||
OutputFlag = &cli.StringFlag{ | ||
Name: "output", | ||
Aliases: []string{"o"}, | ||
Usage: "write signature to a specific path", | ||
} | ||
) |
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,74 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"math" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/notaryproject/nv2/pkg/reference" | ||
"github.com/notaryproject/nv2/pkg/registry" | ||
"github.com/opencontainers/go-digest" | ||
"github.com/urfave/cli/v2" | ||
) | ||
|
||
// GetManifestFromContext reterives the manifest according to CLI context | ||
func GetManifestFromContext(ctx *cli.Context) (*reference.Manifest, error) { | ||
if uri := ctx.Args().First(); uri != "" { | ||
return getManfestsFromURI(ctx, uri) | ||
} | ||
return getManifestFromReader(os.Stdin, ctx.String(MediaTypeFlag.Name)) | ||
} | ||
|
||
func getManifestFromReader(r io.Reader, mediaType string) (*reference.Manifest, error) { | ||
lr := &io.LimitedReader{ | ||
R: r, | ||
N: math.MaxInt64, | ||
} | ||
manifestDigest, err := digest.SHA256.FromReader(lr) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &reference.Manifest{ | ||
Descriptor: reference.Descriptor{ | ||
MediaType: mediaType, | ||
Digests: []digest.Digest{manifestDigest}, | ||
Size: math.MaxInt64 - lr.N, | ||
}, | ||
AccessedAt: time.Now().UTC(), | ||
}, nil | ||
} | ||
|
||
func getManfestsFromURI(ctx *cli.Context, uri string) (*reference.Manifest, error) { | ||
parsed, err := url.Parse(uri) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var r io.Reader | ||
switch strings.ToLower(parsed.Scheme) { | ||
case "file": | ||
path := parsed.Path | ||
if parsed.Opaque != "" { | ||
path = parsed.Opaque | ||
} | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer file.Close() | ||
r = file | ||
case "docker", "oci": | ||
remote := registry.NewClient(nil, ®istry.ClientOptions{ | ||
Username: ctx.String(UsernameFlag.Name), | ||
Password: ctx.String(PasswordFlag.Name), | ||
Insecure: ctx.Bool(InsecureFlag.Name), | ||
}) | ||
return remote.GetManifestMetadata(parsed) | ||
default: | ||
return nil, fmt.Errorf("unsupported URI scheme: %s", parsed.Scheme) | ||
} | ||
return getManifestFromReader(r, ctx.String(MediaTypeFlag.Name)) | ||
} |
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 was deleted.
Oops, something went wrong.
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
Oops, something went wrong.