Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"go.inferGopath": false
}
21 changes: 17 additions & 4 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"oras.land/oras-go/pkg/content"

"github.com/engineerd/wasm-to-oci/pkg/tuf"
)
Expand All @@ -18,13 +19,18 @@ var (
logLevel string
timeout string

insecure bool
useHTTP bool
regopts content.RegistryOptions
)

func main() {

cmd := &cobra.Command{
Use: "wasm-to-oci <subcommand> [options]",

Short: "Manage WebAssemblies on OCI repositories",

Example: "wasm-to-oci <subcommand> [options]",

PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
l, err := log.ParseLevel(logLevel)
if err != nil {
Expand All @@ -40,13 +46,20 @@ func main() {
cmd.PersistentFlags().StringVarP(&trustDir, "dir", "d", defaultTrustDir(), "Directory where the trust data is persisted to")
cmd.PersistentFlags().StringVar(&logLevel, "log", "info", `Set the logging level ("debug"|"info"|"warn"|"error"|"fatal")`)
cmd.PersistentFlags().StringVarP(&timeout, "timeout", "t", "5s", `Timeout for the trust server`)
cmd.PersistentFlags().BoolVarP(&insecure, "insecure", "", false, "Allow connections to SSL registry without certs")
cmd.PersistentFlags().BoolVarP(&useHTTP, "use-http", "", false, "Use plain http instead of https")
cmd.PersistentFlags().StringVarP(&regopts.Username, "username", "u", "", `Username (required)`)
cmd.PersistentFlags().StringVarP(&regopts.Password, "password", "p", "", `Password (required)`)

cmd.PersistentFlags().BoolVarP(&regopts.Insecure, "insecure", "", false, "Allow connections to SSL registry without certs")
cmd.PersistentFlags().BoolVarP(&regopts.PlainHTTP, "use-http", "", false, "Use plain http instead of https")

cmd.MarkPersistentFlagRequired("username")
cmd.MarkPersistentFlagRequired("password")

cmd.AddCommand(newPushCmd(), newPullCmd())
if err := cmd.Execute(); err != nil {
os.Exit(1)
}

}

func defaultTrustDir() string {
Expand Down
3 changes: 2 additions & 1 deletion cmd/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ func newPullCmd() *cobra.Command {
}

func (p *pullOptions) run() error {
err := oci.Pull(p.ref, p.outFile, insecure, useHTTP)

err := oci.Pull(p.ref, p.outFile, regopts)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func (p *pushOptions) run() error {

}

return oci.Push(p.ref, p.module, insecure, useHTTP)
return oci.Push(p.ref, p.module, regopts)
}
26 changes: 19 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@ module github.com/engineerd/wasm-to-oci
go 1.16

require (
github.com/containerd/containerd v1.5.5
github.com/docker/cli v20.10.7+incompatible
github.com/docker/distribution v2.7.1+incompatible
github.com/docker/docker v17.12.1-ce+incompatible // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/hcsshim v0.9.2 // indirect
github.com/containerd/containerd v1.5.9
github.com/containerd/continuity v0.2.2 // indirect
github.com/docker/cli v20.10.12+incompatible
github.com/docker/distribution v2.8.0+incompatible
github.com/docker/docker v20.10.12+incompatible // indirect
github.com/docker/go v1.5.1-1.0.20160303222718-d30aec9fd63c
github.com/opencontainers/image-spec v1.0.1
github.com/klauspost/compress v1.14.2 // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
github.com/opencontainers/image-spec v1.0.2
github.com/opencontainers/runc v1.1.0 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/cobra v1.3.0
github.com/theupdateframework/notary v0.7.0
oras.land/oras-go v0.4.0
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838 // indirect
golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd // indirect
golang.org/x/sys v0.0.0-20220204135822-1c1b9b1eba6a // indirect
google.golang.org/genproto v0.0.0-20220207185906-7721543eae58 // indirect
google.golang.org/grpc v1.44.0 // indirect
oras.land/oras-go v1.1.0
)

replace (
Expand Down
336 changes: 336 additions & 0 deletions go.sum

Large diffs are not rendered by default.

28 changes: 11 additions & 17 deletions pkg/oci/pull.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
package oci

import (
"io/ioutil"

log "github.com/sirupsen/logrus"
"oras.land/oras-go/pkg/content"
"oras.land/oras-go/pkg/oras"
)

// Pull pulls a Wasm module from an OCI registry given a reference
func Pull(ref, outFile string, insecure, useHTTP bool) error {
ctx, resolver, store := newORASContext(insecure, useHTTP)

pullOpts := []oras.PullOpt{
oras.WithAllowedMediaType(ContentLayerMediaType),
oras.WithPullEmptyNameAllowed(),
}

_, layers, err := oras.Pull(ctx, resolver, ref, store, pullOpts...)
func Pull(ref, outFile string, opts content.RegistryOptions) error {
ctx, registry, _ := newORASContext(opts)

// Pull file(s) from registry and save to disk
log.Infof("Pulling from %s and saving to %s...\n", ref, outFile)
fileStore := content.NewFile(outFile)
defer fileStore.Close()
allowedMediaTypes := []string{ContentLayerMediaType}
desc, err := oras.Copy(ctx, registry, ref, fileStore, "", oras.WithAllowedMediaTypes(allowedMediaTypes))
if err != nil {
return err
}

desc := layers[0]
manifest, contents, _ := store.Get(desc)
ioutil.WriteFile(outFile, contents, 0755)

log.Infof("Pulled: %v", ref)
log.Infof("Size: %v", desc.Size)
log.Infof("Digest: %v", manifest.Digest)
log.Infof("Digest: %v", desc.Digest)

return nil
}
33 changes: 19 additions & 14 deletions pkg/oci/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,41 @@ package oci
import (
"io/ioutil"

ocispec "github.com/opencontainers/image-spec/specs-go/v1"
log "github.com/sirupsen/logrus"
"oras.land/oras-go/pkg/content"
"oras.land/oras-go/pkg/oras"
)

// Push pushes a WASM module to an OCI registry
func Push(ref, module string, insecure, useHTTP bool) error {
ctx, resolver, store := newORASContext(insecure, useHTTP)
func Push(ref, module string, opts content.RegistryOptions) error {
ctx, registry, store := newORASContext(opts)

contents, err := ioutil.ReadFile(module)
if err != nil {
return err
}

desc := store.Add(module, ContentLayerMediaType, contents)
layers := []ocispec.Descriptor{desc}

pushOpts := []oras.PushOpt{
oras.WithConfigMediaType(ConfigMediaType),
oras.WithNameValidation(nil),
desc, err := store.Add(module, ContentLayerMediaType, contents)
if err != nil {
return err
}

manifest, err := oras.Push(ctx, resolver, ref, store, layers, pushOpts...)
manifest, manifestDesc, config, configDesc, err := content.GenerateManifestAndConfig(nil, nil, desc)
if err != nil {
return err
}
store.Set(configDesc, config)
err = store.StoreManifest(ref, manifestDesc, manifest)
if err != nil {
return err
}

log.Infof("Pushed: %v", ref)
log.Infof("Pushing %s to %s...\n", module, ref)

desc, err = oras.Copy(ctx, store, ref, registry, "")
if err != nil {
return err
}
log.Infof("Size: %v", desc.Size)
log.Infof("Digest: %v", manifest.Digest)
log.Infof("Pushed to %s with digest %s\n", ref, desc.Digest)

return nil
}
39 changes: 12 additions & 27 deletions pkg/oci/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,25 @@ package oci

import (
"context"
"crypto/tls"
"fmt"
"net/http"
"os"

"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
auth "oras.land/oras-go/pkg/auth/docker"
"oras.land/oras-go/pkg/content"
orascnt "oras.land/oras-go/pkg/content"
orasctx "oras.land/oras-go/pkg/context"
)

func newORASContext(insecure, useHTTP bool) (context.Context, remotes.Resolver, *orascnt.Memorystore) {
ctx := orasctx.Background()
memoryStore := orascnt.NewMemoryStore()
cli, err := auth.NewClient()
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: Error loading auth file: %v\n", err)
func check(e error) {
if e != nil {
panic(e)
}
}

client := http.DefaultClient
if insecure {
client.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
}
func newORASContext(opts content.RegistryOptions) (context.Context, *content.Registry, *orascnt.Memory) {
ctx := orasctx.Background()
memoryStore := orascnt.NewMemory()

resolver, err := cli.Resolver(context.Background(), client, useHTTP)
if err != nil {
fmt.Fprintf(os.Stderr, "WARNING: Error loading resolver: %v\n", err)
resolver = docker.NewResolver(docker.ResolverOptions{})
}
registry, err := content.NewRegistry(opts)

check(err)

return ctx, resolver, memoryStore
return ctx, registry, memoryStore
}
1 change: 1 addition & 0 deletions target/golist.json

Large diffs are not rendered by default.