Skip to content

Commit

Permalink
Fix MFA cases
Browse files Browse the repository at this point in the history
Fixes #20
  • Loading branch information
hypnoglow committed Jan 12, 2018
1 parent df9b83d commit dc67f49
Show file tree
Hide file tree
Showing 10 changed files with 137 additions and 106 deletions.
10 changes: 5 additions & 5 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 13 additions & 11 deletions cmd/helms3/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,28 @@ import (
"github.com/pkg/errors"

"github.com/hypnoglow/helm-s3/pkg/awss3"
"github.com/hypnoglow/helm-s3/pkg/awsutil"
"github.com/hypnoglow/helm-s3/pkg/helmutil"
"github.com/hypnoglow/helm-s3/pkg/index"
)

func runDelete(name, version, repoName string) error {
repoEntry, err := helmutil.LookupRepoEntry(repoName)
type deleteAction struct {
name, version, repoName string
}

func (act deleteAction) Run(ctx context.Context) error {
repoEntry, err := helmutil.LookupRepoEntry(act.repoName)
if err != nil {
return err
}

storage := awss3.New()
sess, err := awsutil.Session()
if err != nil {
return err
}
storage := awss3.New(sess)

// Fetch current index.

ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()

b, err := storage.FetchRaw(ctx, repoEntry.URL+"/index.yaml")
if err != nil {
return errors.WithMessage(err, "fetch current repo index")
Expand All @@ -36,7 +41,7 @@ func runDelete(name, version, repoName string) error {

// Update index.

chartVersion, err := idx.Delete(name, version)
chartVersion, err := idx.Delete(act.name, act.version)
if err != nil {
return err
}
Expand All @@ -53,9 +58,6 @@ func runDelete(name, version, repoName string) error {
}
uri := chartVersion.URLs[0]

ctx, cancel = context.WithTimeout(context.Background(), defaultTimeout*2)
defer cancel()

if err := storage.Delete(ctx, uri); err != nil {
return errors.WithMessage(err, "delete chart file from s3")
}
Expand Down
18 changes: 12 additions & 6 deletions cmd/helms3/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,27 @@ import (
"github.com/pkg/errors"

"github.com/hypnoglow/helm-s3/pkg/awss3"
"github.com/hypnoglow/helm-s3/pkg/awsutil"
"github.com/hypnoglow/helm-s3/pkg/index"
)

func runInit(uri string) error {
type initAction struct {
uri string
}

func (act initAction) Run(ctx context.Context) error {
r, err := index.New().Reader()
if err != nil {
return errors.WithMessage(err, "get index reader")
}

storage := awss3.New()

ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
sess, err := awsutil.Session()
if err != nil {
return err
}
storage := awss3.New(sess)

if err := storage.PutIndex(ctx, uri, r); err != nil {
if err := storage.PutIndex(ctx, act.uri, r); err != nil {
return errors.WithMessage(err, "upload index to s3")
}

Expand Down
47 changes: 33 additions & 14 deletions cmd/helms3/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"log"
"os"
Expand All @@ -20,12 +21,20 @@ const (
actionReindex = "reindex"
actionDelete = "delete"

defaultTimeout = time.Second * 5
defaultTimeout = time.Minute * 5
)

// Action describes plugin action that can be run.
type Action interface {
Run(context.Context) error
}

func main() {
if len(os.Args) == 5 {
if err := runProxy(os.Args[4]); err != nil {
cmd := proxyCmd{uri: os.Args[4]}
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
if err := cmd.Run(ctx); err != nil {
log.Fatal(err)
}
return
Expand Down Expand Up @@ -69,35 +78,45 @@ func main() {
os.Exit(0)
}

var act Action
switch action {
case actionVersion:
fmt.Print(version)
return

case actionInit:
if err := runInit(*initURI); err != nil {
log.Fatal(err)
act = initAction{
uri: *initURI,
}
fmt.Printf("Initialized empty repository at %s\n", *initURI)
return
defer fmt.Printf("Initialized empty repository at %s\n", *initURI)

case actionPush:
if err := runPush(*pushChartPath, *pushTargetRepository); err != nil {
log.Fatal(err)
act = pushAction{
chartPath: *pushChartPath,
repoName: *pushTargetRepository,
}
return

case actionReindex:
fmt.Fprint(os.Stderr, "Warning: reindex feature is in beta. If you experience any issues,\nplease provide your feedback here: https://github.com/hypnoglow/helm-s3/issues/22\n\n")
if err := runReindex(*reindexTargetRepository); err != nil {
log.Fatal(err)
act = reindexAction{
repoName: *reindexTargetRepository,
}
fmt.Printf("Repository %s was successfully reindexed.\n", *reindexTargetRepository)
defer fmt.Printf("Repository %s was successfully reindexed.\n", *reindexTargetRepository)

case actionDelete:
if err := runDelete(*deleteChartName, *deleteChartVersion, *deleteTargetRepository); err != nil {
log.Fatal(err)
act = deleteAction{
name: *deleteChartName,
version: *deleteChartVersion,
repoName: *deleteTargetRepository,
}
default:
return
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()

if err := act.Run(ctx); err != nil {
log.Fatal(err)
}
}
21 changes: 13 additions & 8 deletions cmd/helms3/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@ import (
"github.com/pkg/errors"

"github.com/hypnoglow/helm-s3/pkg/awss3"
"github.com/hypnoglow/helm-s3/pkg/awsutil"
)

func runProxy(uri string) error {

storage := awss3.New()
type proxyCmd struct {
uri string
}

ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
func (act proxyCmd) Run(ctx context.Context) error {
sess, err := awsutil.Session(awsutil.AssumeRoleTokenProvider(awsutil.StderrTokenProvider))
if err != nil {
return err
}
storage := awss3.New(sess)

b, err := storage.FetchRaw(ctx, uri)
b, err := storage.FetchRaw(ctx, act.uri)
if err != nil {
if strings.HasSuffix(uri, "index.yaml") && err == awss3.ErrObjectNotFound {
return fmt.Errorf("The index file does not exist by the path %s. If you haven't initialized the repository yet, try running \"helm s3 init %s\"", uri, path.Dir(uri))
if strings.HasSuffix(act.uri, "index.yaml") && err == awss3.ErrObjectNotFound {
return fmt.Errorf("The index file does not exist by the path %s. If you haven't initialized the repository yet, try running \"helm s3 init %s\"", act.uri, path.Dir(act.uri))
}
return errors.WithMessage(err, "fetch from s3")
}
Expand Down
25 changes: 13 additions & 12 deletions cmd/helms3/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,30 @@ import (
"fmt"
"os"
"path/filepath"
"time"

"github.com/pkg/errors"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/provenance"

"github.com/hypnoglow/helm-s3/pkg/awss3"
"github.com/hypnoglow/helm-s3/pkg/awsutil"
"github.com/hypnoglow/helm-s3/pkg/helmutil"
"github.com/hypnoglow/helm-s3/pkg/index"
)

const (
pushCommandDefaultTimeout = time.Second * 15
)
type pushAction struct {
chartPath string
repoName string
}

func runPush(chartPath string, repoName string) error {
// Just one big timeout for the whole operation.
ctx, cancel := context.WithTimeout(context.Background(), pushCommandDefaultTimeout)
defer cancel()
func (act pushAction) Run(ctx context.Context) error {
sess, err := awsutil.Session()
if err != nil {
return err
}
storage := awss3.New(sess)

fpath, err := filepath.Abs(chartPath)
fpath, err := filepath.Abs(act.chartPath)
if err != nil {
return errors.WithMessage(err, "get chart abs path")
}
Expand All @@ -38,8 +41,6 @@ func runPush(chartPath string, repoName string) error {
return errors.Wrapf(err, "change dir to %s", dir)
}

storage := awss3.New()

// Load chart, calculate required params like hash,
// and upload the chart right away.

Expand All @@ -48,7 +49,7 @@ func runPush(chartPath string, repoName string) error {
return fmt.Errorf("file %s is not a helm chart archive", fname)
}

repoEntry, err := helmutil.LookupRepoEntry(repoName)
repoEntry, err := helmutil.LookupRepoEntry(act.repoName)
if err != nil {
return err
}
Expand Down
22 changes: 11 additions & 11 deletions cmd/helms3/reindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,30 @@ package main

import (
"context"
"time"

"github.com/pkg/errors"

"github.com/hypnoglow/helm-s3/pkg/awss3"
"github.com/hypnoglow/helm-s3/pkg/awsutil"
"github.com/hypnoglow/helm-s3/pkg/helmutil"
"github.com/hypnoglow/helm-s3/pkg/index"
)

const (
reindexCommandDefaultTimeout = time.Second * 15
)

func runReindex(repoName string) error {
// Just one big timeout for the whole operation.
ctx, cancel := context.WithTimeout(context.Background(), reindexCommandDefaultTimeout)
defer cancel()
type reindexAction struct {
repoName string
}

repoEntry, err := helmutil.LookupRepoEntry(repoName)
func (act reindexAction) Run(ctx context.Context) error {
repoEntry, err := helmutil.LookupRepoEntry(act.repoName)
if err != nil {
return err
}

storage := awss3.New()
sess, err := awsutil.Session()
if err != nil {
return err
}
storage := awss3.New(sess)

items, errs := storage.Traverse(ctx, repoEntry.URL)

Expand Down
Loading

0 comments on commit dc67f49

Please sign in to comment.