Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storage profiles with tokens #688

Merged
merged 2 commits into from
Oct 21, 2020
Merged
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
12 changes: 6 additions & 6 deletions api/client/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ type Admin struct {
client proto.PowergateAdminServiceClient
}

// CreateInstance creates a new FFS instance, returning the instance ID and auth token.
func (a *Admin) CreateInstance(ctx context.Context) (*proto.CreateInstanceResponse, error) {
return a.client.CreateInstance(ctx, &proto.CreateInstanceRequest{})
// CreateStorageProfile creates a new Powergate storage profile, returning the instance ID and auth token.
func (a *Admin) CreateStorageProfile(ctx context.Context) (*proto.CreateStorageProfileResponse, error) {
return a.client.CreateStorageProfile(ctx, &proto.CreateStorageProfileRequest{})
}

// ListInstances returns a list of existing API instances.
func (a *Admin) ListInstances(ctx context.Context) (*proto.ListInstancesResponse, error) {
return a.client.ListInstances(ctx, &proto.ListInstancesRequest{})
// ListStorageProfiles returns a list of existing API instances.
func (a *Admin) ListStorageProfiles(ctx context.Context) (*proto.ListStorageProfilesResponse, error) {
return a.client.ListStorageProfiles(ctx, &proto.ListStorageProfilesRequest{})
}

// QueuedStorageJobs returns a list of queued storage jobs.
Expand Down
16 changes: 8 additions & 8 deletions api/client/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ func TestCreate(t *testing.T) {
a, done := setupAdmin(t, "")
defer done()

resp, err := a.CreateInstance(ctx)
resp, err := a.CreateStorageProfile(ctx)
require.NoError(t, err)
require.NotEmpty(t, resp.Id)
require.NotEmpty(t, resp.Token)
require.NotEmpty(t, resp.AuthEntry.Id)
require.NotEmpty(t, resp.AuthEntry.Token)
})

t.Run("WithAdminToken", func(t *testing.T) {
Expand All @@ -28,7 +28,7 @@ func TestCreate(t *testing.T) {
defer done()

t.Run("UnauthorizedEmpty", func(t *testing.T) {
resp, err := a.CreateInstance(ctx)
resp, err := a.CreateStorageProfile(ctx)
require.Error(t, err)
require.Nil(t, resp)
})
Expand All @@ -40,7 +40,7 @@ func TestCreate(t *testing.T) {
}
for _, auth := range wrongAuths {
ctx := context.WithValue(ctx, AdminKey, auth)
resp, err := a.CreateInstance(ctx)
resp, err := a.CreateStorageProfile(ctx)
st, ok := status.FromError(err)
require.True(t, ok)
require.Equal(t, codes.PermissionDenied, st.Code())
Expand All @@ -49,10 +49,10 @@ func TestCreate(t *testing.T) {
})
t.Run("Authorized", func(t *testing.T) {
ctx := context.WithValue(ctx, AdminKey, authToken)
resp, err := a.CreateInstance(ctx)
resp, err := a.CreateStorageProfile(ctx)
require.NoError(t, err)
require.NotEmpty(t, resp.Id)
require.NotEmpty(t, resp.Token)
require.NotEmpty(t, resp.AuthEntry.Id)
require.NotEmpty(t, resp.AuthEntry.Token)
})
})
}
Expand Down
36 changes: 0 additions & 36 deletions api/server/admin/instances.go

This file was deleted.

41 changes: 41 additions & 0 deletions api/server/admin/storageprofiles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package admin

import (
"context"

proto "github.com/textileio/powergate/proto/admin/v1"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)

// CreateStorageProfile creates a new managed instance.
func (a *Service) CreateStorageProfile(ctx context.Context, req *proto.CreateStorageProfileRequest) (*proto.CreateStorageProfileResponse, error) {
id, token, err := a.m.Create(ctx)
if err != nil {
return nil, status.Errorf(codes.Internal, "creating instance: %v", err)
}
return &proto.CreateStorageProfileResponse{
AuthEntry: &proto.AuthEntry{
Id: id.String(),
Token: token,
},
}, nil
}

// ListStorageProfiles lists all managed instances.
func (a *Service) ListStorageProfiles(ctx context.Context, req *proto.ListStorageProfilesRequest) (*proto.ListStorageProfilesResponse, error) {
lst, err := a.m.List()
if err != nil {
return nil, status.Errorf(codes.Internal, "listing storage profiles: %v", err)
}
ins := make([]*proto.AuthEntry, len(lst))
for i, v := range lst {
ins[i] = &proto.AuthEntry{
Id: v.APIID.String(),
Token: v.Token,
}
}
return &proto.ListStorageProfilesResponse{
AuthEntries: ins,
}, nil
}
4 changes: 2 additions & 2 deletions cli-docs/pow/pow_admin.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ Provides admin commands
### SEE ALSO

* [pow](pow.md) - A client for storage and retreival of powergate data
* [pow admin create-instance](pow_admin_create-instance.md) - Create a powergate FFS instance.
* [pow admin create-profile](pow_admin_create-profile.md) - Create a Powergate storage profile.
* [pow admin executing](pow_admin_executing.md) - List executing storage jobs
* [pow admin instances](pow_admin_instances.md) - List all powergate FFS instances.
* [pow admin latest-final](pow_admin_latest-final.md) - List the latest final storage jobs
* [pow admin latest-successful](pow_admin_latest-successful.md) - List the latest successful storage jobs
* [pow admin profiles](pow_admin_profiles.md) - List all Powergate storage profiles.
* [pow admin queued](pow_admin_queued.md) - List queued storage jobs
* [pow admin summary](pow_admin_summary.md) - Give a summary of storage jobs in all states

Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
## pow admin create-instance
## pow admin create-profile

Create a powergate FFS instance.
Create a Powergate storage profile.

### Synopsis

Create a powergate FFS instance.
Create a Powergate storage profile.

```
pow admin create-instance [flags]
pow admin create-profile [flags]
```

### Options

```
-h, --help help for create-instance
-h, --help help for create-profile
```

### Options inherited from parent commands
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
## pow admin instances
## pow admin profiles

List all powergate FFS instances.
List all Powergate storage profiles.

### Synopsis

List all powergate FFS instances.
List all Powergate storage profiles.

```
pow admin instances [flags]
pow admin profiles [flags]
```

### Options

```
-h, --help help for instances
-h, --help help for profiles
```

### Options inherited from parent commands
Expand Down
16 changes: 8 additions & 8 deletions cmd/pow/cmd/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ var adminCmd = &cobra.Command{
}

var adminCreateInstanceCmd = &cobra.Command{
Use: "create-instance",
Short: "Create a powergate FFS instance.",
Long: `Create a powergate FFS instance.`,
Use: "create-profile",
Short: "Create a Powergate storage profile.",
Long: `Create a Powergate storage profile.`,
Args: cobra.NoArgs,
PreRun: func(cmd *cobra.Command, args []string) {
err := viper.BindPFlags(cmd.Flags())
Expand All @@ -58,7 +58,7 @@ var adminCreateInstanceCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
defer cancel()

res, err := fcClient.Admin.CreateInstance(adminAuthCtx(ctx))
res, err := fcClient.Admin.CreateStorageProfile(adminAuthCtx(ctx))
checkErr(err)

json, err := protojson.MarshalOptions{Multiline: true, Indent: " ", EmitUnpopulated: true}.Marshal(res)
Expand All @@ -69,9 +69,9 @@ var adminCreateInstanceCmd = &cobra.Command{
}

var adminInstancesCmd = &cobra.Command{
Use: "instances",
Short: "List all powergate FFS instances.",
Long: `List all powergate FFS instances.`,
Use: "profiles",
Short: "List all Powergate storage profiles.",
Long: `List all Powergate storage profiles.`,
Args: cobra.NoArgs,
PreRun: func(cmd *cobra.Command, args []string) {
err := viper.BindPFlags(cmd.Flags())
Expand All @@ -81,7 +81,7 @@ var adminInstancesCmd = &cobra.Command{
ctx, cancel := context.WithTimeout(context.Background(), cmdTimeout)
defer cancel()

res, err := fcClient.Admin.ListInstances(adminAuthCtx(ctx))
res, err := fcClient.Admin.ListStorageProfiles(adminAuthCtx(ctx))
checkErr(err)

json, err := protojson.MarshalOptions{Multiline: true, Indent: " ", EmitUnpopulated: true}.Marshal(res)
Expand Down
4 changes: 2 additions & 2 deletions cmd/powbench/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func sanityCheck(ctx context.Context, c *client.Client) error {
}

func runSetup(ctx context.Context, c *client.Client, ts TestSetup) error {
res, err := c.Admin.CreateInstance(ctx)
res, err := c.Admin.CreateStorageProfile(ctx)
if err != nil {
return fmt.Errorf("creating ffs instance: %s", err)
}
ctx = context.WithValue(ctx, client.AuthKey, res.Token)
ctx = context.WithValue(ctx, client.AuthKey, res.AuthEntry.Token)
res2, err := c.FFS.Addrs(ctx)
if err != nil {
return fmt.Errorf("getting instance info: %s", err)
Expand Down
18 changes: 6 additions & 12 deletions ffs/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ type Auth struct {
ds ds.Datastore
}

type entry struct {
Token string
APIID ffs.APIID
// This can be extended to have permissions
}

// New returns a new Auth.
func New(store ds.Datastore) *Auth {
return &Auth{
Expand All @@ -44,7 +38,7 @@ func (r *Auth) Generate(iid ffs.APIID) (string, error) {
log.Infof("generating auth-token for instance %s", iid)
r.lock.Lock()
defer r.lock.Unlock()
e := entry{
e := ffs.AuthEntry{
Token: uuid.New().String(),
APIID: iid,
}
Expand All @@ -71,15 +65,15 @@ func (r *Auth) Get(token string) (ffs.APIID, error) {
if err != nil {
return ffs.EmptyInstanceID, fmt.Errorf("getting token %s from datastore: %s", token, err)
}
var e entry
var e ffs.AuthEntry
if err := json.Unmarshal(buf, &e); err != nil {
return ffs.EmptyInstanceID, fmt.Errorf("unmarshaling %s information from datastore: %s", token, err)
}
return e.APIID, nil
}

// List returns a list of all API instances.
func (r *Auth) List() ([]ffs.APIID, error) {
func (r *Auth) List() ([]ffs.AuthEntry, error) {
r.lock.Lock()
defer r.lock.Unlock()
q := query.Query{Prefix: ""}
Expand All @@ -93,16 +87,16 @@ func (r *Auth) List() ([]ffs.APIID, error) {
}
}()

var ret []ffs.APIID
var ret []ffs.AuthEntry
for r := range res.Next() {
if r.Error != nil {
return nil, fmt.Errorf("iter next: %s", r.Error)
}
var e entry
var e ffs.AuthEntry
if err := json.Unmarshal(r.Entry.Value, &e); err != nil {
return nil, fmt.Errorf("unmarshaling query result: %s", err)
}
ret = append(ret, e.APIID)
ret = append(ret, e)
}
return ret, nil
}
2 changes: 1 addition & 1 deletion ffs/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func (m *Manager) SetDefaultStorageConfig(dc ffs.StorageConfig) error {
}

// List returns a list of all existing API instances.
func (m *Manager) List() ([]ffs.APIID, error) {
func (m *Manager) List() ([]ffs.AuthEntry, error) {
m.lock.Lock()
defer m.lock.Unlock()

Expand Down
6 changes: 6 additions & 0 deletions ffs/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ func (i APIID) String() string {
return string(i)
}

// AuthEntry encapsulates auth info for a FFS instance.
type AuthEntry struct {
Token string
APIID APIID
}

// JobStatus is a type for Job statuses.
type JobStatus int

Expand Down
Loading