2019-06-27 18:36:51 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package uplink_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
|
|
|
"storj.io/storj/lib/uplink"
|
|
|
|
"storj.io/storj/pkg/macaroon"
|
|
|
|
)
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
func RestrictAccessExampleByAdmin(ctx context.Context,
|
|
|
|
satelliteAddress, apiKey, adminAccess string,
|
|
|
|
cfg *uplink.Config, out io.Writer) (
|
2019-07-02 16:45:23 +01:00
|
|
|
serializedScope string, err error) {
|
2019-06-28 14:40:37 +01:00
|
|
|
|
|
|
|
// Parse the API key. API keys are "macaroons" that allow you to create new,
|
|
|
|
// restricted API keys.
|
2019-06-27 18:36:51 +01:00
|
|
|
key, err := uplink.ParseAPIKey(apiKey)
|
|
|
|
if err != nil {
|
2019-07-02 16:45:23 +01:00
|
|
|
return "", err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// Restrict the API key to be read only and to be for just the prod and
|
|
|
|
// staging buckets for the path webserver/logs/
|
2019-06-27 18:36:51 +01:00
|
|
|
userAPIKey, err := key.Restrict(macaroon.Caveat{
|
|
|
|
DisallowWrites: true,
|
|
|
|
DisallowDeletes: true,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2019-07-02 16:45:23 +01:00
|
|
|
return "", err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// Load the existing encryption access context
|
2019-06-28 06:18:24 +01:00
|
|
|
access, err := uplink.ParseEncryptionAccess(adminAccess)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-02 16:45:23 +01:00
|
|
|
return "", err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// Restrict the encryption access context to just the prod and staging
|
|
|
|
// buckets for webserver/logs/
|
2019-06-28 06:18:24 +01:00
|
|
|
userAPIKey, userAccess, err := access.Restrict(userAPIKey,
|
2019-06-28 14:40:37 +01:00
|
|
|
uplink.EncryptionRestriction{
|
|
|
|
Bucket: "prod",
|
|
|
|
PathPrefix: "webserver/logs",
|
|
|
|
},
|
|
|
|
uplink.EncryptionRestriction{
|
|
|
|
Bucket: "staging",
|
|
|
|
PathPrefix: "webserver/logs",
|
|
|
|
},
|
2019-06-27 18:36:51 +01:00
|
|
|
)
|
|
|
|
if err != nil {
|
2019-07-02 16:45:23 +01:00
|
|
|
return "", err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-07-02 16:45:23 +01:00
|
|
|
userScope := &uplink.Scope{
|
|
|
|
SatelliteAddr: satelliteAddress,
|
|
|
|
APIKey: userAPIKey,
|
|
|
|
EncryptionAccess: userAccess,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Serialize the scope
|
|
|
|
serializedScope, err = userScope.Serialize()
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
2019-07-02 16:45:23 +01:00
|
|
|
return "", err
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Fprintln(out, "success!")
|
2019-07-02 16:45:23 +01:00
|
|
|
return serializedScope, nil
|
2019-06-27 18:36:51 +01:00
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
func RestrictAccessExampleByUser(ctx context.Context,
|
2019-07-02 16:45:23 +01:00
|
|
|
serializedScope string, cfg *uplink.Config, out io.Writer) (err error) {
|
2019-06-27 18:36:51 +01:00
|
|
|
errCatch := func(fn func() error) { err = errs.Combine(err, fn()) }
|
|
|
|
|
|
|
|
// First, create an Uplink handle.
|
|
|
|
ul, err := uplink.NewUplink(ctx, cfg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer errCatch(ul.Close)
|
|
|
|
|
2019-07-02 16:45:23 +01:00
|
|
|
// Parse the scope.
|
|
|
|
scope, err := uplink.ParseScope(serializedScope)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// Open the project in question. Projects are identified by a specific
|
|
|
|
// Satellite and API key
|
2019-07-02 16:45:23 +01:00
|
|
|
p, err := ul.OpenProject(ctx, scope.SatelliteAddr, scope.APIKey)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer errCatch(p.Close)
|
|
|
|
|
|
|
|
// Open bucket
|
2019-07-02 16:45:23 +01:00
|
|
|
bucket, err := p.OpenBucket(ctx, "prod", scope.EncryptionAccess)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer errCatch(bucket.Close)
|
|
|
|
|
|
|
|
// Open file
|
|
|
|
obj, err := bucket.OpenObject(ctx, "webserver/logs/log.txt")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer errCatch(obj.Close)
|
|
|
|
|
|
|
|
// Get a reader for the entire file
|
|
|
|
r, err := obj.DownloadRange(ctx, 0, -1)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer errCatch(r.Close)
|
|
|
|
|
|
|
|
// Read the file
|
|
|
|
data, err := ioutil.ReadAll(r)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Print it!
|
|
|
|
fmt.Fprintln(out, string(data))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Example_restrictAccess() {
|
2019-06-28 14:40:37 +01:00
|
|
|
// The satellite address is the address of the satellite your API key is
|
|
|
|
// valid on
|
2019-06-27 18:36:51 +01:00
|
|
|
satelliteAddress := "us-central-1.tardigrade.io:7777"
|
|
|
|
|
|
|
|
// The API key can be created in the web interface
|
|
|
|
adminAPIKey := "qPSUM3k0bZyOIyil2xrVWiSuc9HuB2yBP3qDrA2Gc"
|
|
|
|
|
2019-06-28 14:40:37 +01:00
|
|
|
// The encryption access context was created using
|
|
|
|
// NewEncryptionAccessWithDefaultKey and
|
2019-06-27 18:36:51 +01:00
|
|
|
// (*Project).SaltedKeyFromPassphrase() earlier
|
2019-06-28 06:18:24 +01:00
|
|
|
adminAccess := "HYGoqCEz43mCE40Hc5lQD3DtUYynx9Vo1GjOx75hQ"
|
2019-06-27 18:36:51 +01:00
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2019-07-02 16:45:23 +01:00
|
|
|
// Admin1 is going to create a scope and share it
|
|
|
|
userScope, err := RestrictAccessExampleByAdmin(ctx, satelliteAddress,
|
|
|
|
adminAPIKey, adminAccess, &uplink.Config{}, os.Stdout)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-07-02 16:45:23 +01:00
|
|
|
// Admin2 is going to use the provided scope to load the uploaded file
|
|
|
|
err = RestrictAccessExampleByUser(ctx, userScope, &uplink.Config{}, os.Stdout)
|
2019-06-27 18:36:51 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|