Skip to content

Commit

Permalink
Feat: Support for Pinning to Skynet using single account
Browse files Browse the repository at this point in the history
Signed-off-by: jay-dee7 <jasdeepsingh.uppal@gmail.com>
  • Loading branch information
jay-dee7 committed Dec 10, 2021
1 parent 85b6b7d commit 64236eb
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 24 deletions.
7 changes: 6 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type (
EndpointPath string `mapstructure:"endpoint_path"`
ApiKey string `mapstructure:"api_key"`
CustomUserAgent string `mapstructure:"custom_user_agent"`
CustomCookie string `mapstructure:"custom_cookie"`
}

LogConfig struct {
Expand Down Expand Up @@ -66,7 +67,11 @@ func LoadFromENV() (*RegistryConfig, error) {
SkynetPortalURL: viper.GetString("SKYNET_PORTAL_URL"),
SigningSecret: viper.GetString("SIGNING_SECRET"),
DNSAddress: viper.GetString("DNS_ADDRESS"),
SkynetConfig: SkynetConfig{},
SkynetConfig: SkynetConfig{
ApiKey: viper.GetString("SKYNET_API_KEY"),
CustomUserAgent: fmt.Sprintf("OpenRegistry-%s", viper.GetString("VERSION")),
CustomCookie: viper.GetString("SKYNET_CUSTOM_COOKIE"),
},
AuthConfig: AuthConfig{
SupportedServices: make(map[string]bool),
},
Expand Down
4 changes: 3 additions & 1 deletion env-vars.example
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
OPEN_REGISTRY_DEBUG=true
OPEN_REGISTRY_DOMAIN=beta.openregistry.dev
OPEN_REGISTRY_VERSION="master"
OPEN_REGISTRY_HOST=0.0.0.0
OPEN_REGISTRY_PORT=5000
OPEN_REGISTRY_SKYNET_PORTAL_URL=https://siasky.net
OPEN_REGISTRY_SIGNING_SECRET="3tYnaKp@^%hbQA%J&x3cX!r2#mK%EBfAbTvPMv5CU2DP7bAoQGnUfT2&dW"
OPEN_REGISTRY_SIGNING_SECRET="<signin-secret-here>"
OPEN_REGISTRY_SUPPORTED_SERVICES=github,token
OPEN_REGISTRY_ENVIRONMENT=local
OPEN_REGISTRY_LOG_SERVICE_NAME=grafana-loki
OPEN_REGISTRY_LOG_SERVICE_HOST=http://0.0.0.0:9880/app.log
OPEN_REGISTRY_LOG_SERVICE_AUTH_KIND=basic
OPEN_REGISTRY_SKYNET_API_KEY="<skynet-jwt-here>"
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,4 @@ require (
gopkg.in/yaml.v2 v2.4.0 // indirect
)

replace github.com/SkynetLabs/go-skynet/v2 => github.com/containerish/go-skynet/v2 v2.0.2-0.20211205085848-1bbf98d18f7c
replace github.com/SkynetLabs/go-skynet/v2 => github.com/containerish/go-skynet/v2 v2.0.2-0.20211206044851-95b8ecc10d44
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ github.com/cncf/udpa/go v0.0.0-20200629203442-efcf912fb354/go.mod h1:WmhPx2Nbnht
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
github.com/cockroachdb/datadriven v0.0.0-20190809214429-80d97fb3cbaa/go.mod h1:zn76sxSg3SzpJ0PPJaLDCu+Bu0Lg3sKTORVIj19EIF8=
github.com/codahale/hdrhistogram v0.0.0-20161010025455-3a0bb77429bd/go.mod h1:sE/e/2PUdi/liOCUjSTXgM1o87ZssimdTWN964YiIeI=
github.com/containerish/go-skynet/v2 v2.0.2-0.20211205085848-1bbf98d18f7c h1:RHWAHjh7/wBuXnc/B4+tNoV9JMHiH7Wlp45vEttWd1o=
github.com/containerish/go-skynet/v2 v2.0.2-0.20211205085848-1bbf98d18f7c/go.mod h1:XOk0zwGlXeGjHQgmhXTEk7qTD6FVv3dXPW38Wh3XsIc=
github.com/containerish/go-skynet/v2 v2.0.2-0.20211206044851-95b8ecc10d44 h1:0oS0IvCItos8SeUg6hFGI6duDGiIQtx6lcLXKtvBggU=
github.com/containerish/go-skynet/v2 v2.0.2-0.20211206044851-95b8ecc10d44/go.mod h1:XOk0zwGlXeGjHQgmhXTEk7qTD6FVv3dXPW38Wh3XsIc=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
github.com/coreos/go-semver v0.2.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
Expand Down
8 changes: 4 additions & 4 deletions registry/v2/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (r *registry) MonolithicUpload(ctx echo.Context) error {
}
ctx.Request().Body.Close()

link, err := r.skynet.Upload(namespace, digest, bz)
link, err := r.skynet.Upload(namespace, digest, bz, true)
if err != nil {
detail := echo.Map{
"error": err.Error(),
Expand Down Expand Up @@ -242,7 +242,7 @@ func (r *registry) CompleteUpload(ctx echo.Context) error {
}

blobNamespace := fmt.Sprintf("%s/blobs", namespace)
skylink, err := r.skynet.Upload(blobNamespace, dig, buf.Bytes())
skylink, err := r.skynet.Upload(blobNamespace, dig, buf.Bytes(), true)
if err != nil {
errMsg := r.errorResponse(RegistryErrorCodeBlobUploadInvalid, err.Error(), nil)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
Expand Down Expand Up @@ -446,7 +446,7 @@ func (r *registry) PushManifest(ctx echo.Context) error {
}

mfNamespace := fmt.Sprintf("%s/manifests", namespace)
skylink, err := r.skynet.Upload(mfNamespace, dig, bz)
skylink, err := r.skynet.Upload(mfNamespace, dig, bz, true)
if err != nil {
errMsg := r.errorResponse(RegistryErrorCodeManifestBlobUnknown, err.Error(), nil)
ctx.Set(types.HttpEndpointErrorKey, errMsg)
Expand Down Expand Up @@ -616,7 +616,7 @@ func (r *registry) StartUpload(ctx echo.Context) error {
return ctx.JSONBlob(http.StatusBadRequest, errMsg)
}

skylink, err := r.skynet.Upload(namespace, dig, bz)
skylink, err := r.skynet.Upload(namespace, dig, bz, true)
if err != nil {
errMsg := r.errorResponse(RegistryErrorCodeBlobUploadInvalid, err.Error(), nil)
lm := logMsg{
Expand Down
56 changes: 42 additions & 14 deletions skynet/skynet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,44 @@ package skynet

import (
"bytes"
"context"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"time"

"github.com/SkynetLabs/go-skynet/v2"
"github.com/containerish/OpenRegistry/config"
"github.com/fatih/color"
tar "github.com/whyrusleeping/tar-utils"
)

func NewClient(c *config.RegistryConfig) *Client {

opts := skynet.Options{
CustomUserAgent: c.SkynetConfig.CustomUserAgent,
APIKey: c.SkynetConfig.ApiKey,
CustomCookie: c.SkynetConfig.ApiKey,
}

skynetClient := skynet.NewCustom(c.SkynetPortalURL, opts)
//skynetClient := skynet.New()
httpClient := http.DefaultClient
httpClient.Timeout = time.Second * 60

return &Client{
skynet: &skynetClient,
httpClient: httpClient,
isRemote: false,
host: c.Host,
gatewayURL: c.SkynetPortalURL,
}
}

func (c *Client) Upload(namespace, digest string, content []byte, headers ...skynet.Header) (string, error) {
func (c *Client) Upload(namespace, digest string, content []byte, pin bool) (string, error) {
opts := skynet.DefaultUploadOptions
opts.APIKey = c.skynet.Options.APIKey

opts.CustomDirname = namespace

Expand All @@ -39,19 +49,29 @@ func (c *Client) Upload(namespace, digest string, content []byte, headers ...sky

data[digest] = buf

return c.skynet.Upload(data, opts, headers...)
skylink, err := c.skynet.Upload(data, opts)
if err != nil {
return "", err
}

if pin {
time.Sleep(time.Second * 10)
return c.skynet.PinSkylink(skylink)
}

return skylink, nil
}

func (c *Client) Download(path string, headers ...skynet.Header) (io.ReadCloser, error) {
func (c *Client) Download(path string) (io.ReadCloser, error) {
opts := skynet.DefaultDownloadOptions

return c.skynet.Download(path, opts, headers...)
return c.skynet.Download(path, opts)
}

func (c *Client) DownloadDir(skynetLink, dir string, headers ...skynet.Header) error {
func (c *Client) DownloadDir(skynetLink, dir string) error {
opts := skynet.DefaultDownloadOptions

tarball, err := c.skynet.Download(skynetLink, opts, headers...)
tarball, err := c.skynet.Download(skynetLink, opts)
if err != nil {
return err
}
Expand All @@ -67,7 +87,7 @@ func (c *Client) List(path string) ([]*SkynetMeta, error) {
}

// AddImage - arguments: ns = namespace, mf = manifest and l = layers
func (c *Client) AddImage(ns string, mf, l map[string][]byte, headers ...skynet.Header) (string, error) {
func (c *Client) AddImage(ns string, mf, l map[string][]byte) (string, error) {
opts := skynet.DefaultUploadOptions
opts.CustomDirname = ns

Expand All @@ -80,19 +100,26 @@ func (c *Client) AddImage(ns string, mf, l map[string][]byte, headers ...skynet.

uploadData["image"] = imageReader

link, err := c.skynet.Upload(uploadData, opts, headers...)
link, err := c.skynet.Upload(uploadData, opts)
return link, err
}

func (c *Client) Metadata(skylink string) (uint64, bool) {
opts := skynet.DefaultMetadataOptions
info, err := c.skynet.Metadata(skylink, opts)
skl := strings.TrimPrefix(skylink, "sia://")
url := fmt.Sprintf("%s/%s", c.skynet.PortalURL, skl)
req, err := http.NewRequestWithContext(context.Background(), http.MethodHead, url, nil)
if err != nil {
return 0, false
}

// req.SetBasicAuth("", c.skynet.Options.APIKey)
resp, err := c.httpClient.Do(req)
if err != nil {
log.Printf("error getting metadat: %s", err)
return 0, false
}
defer resp.Body.Close()

ct := info.Get("content-length")
ct := resp.Header.Get("Content-Length")
if ct == "" {
return 0, false
}
Expand All @@ -101,6 +128,7 @@ func (c *Client) Metadata(skylink string) (uint64, bool) {
if err != nil {
return 0, false
}
color.Red(skl, contentLength)

return contentLength, true
}
4 changes: 3 additions & 1 deletion skynet/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ import (
"bytes"
"encoding/json"
"io"
"net/http"

skynet "github.com/SkynetLabs/go-skynet/v2"
)

type (
Client struct {
skynet *skynet.SkynetClient
isRemote bool
httpClient *http.Client
host string
gatewayURL string
isRemote bool
}
Config struct {
Host string
Expand Down

0 comments on commit 64236eb

Please sign in to comment.