storj/lib/uplink/access.go
Dylan Lott 6bf46e80ee
Adds Libuplink (#1452)
* Merge in upstream

* Some initial wireup

* Added common.go file, more misc. work

* WIP adding identity in

* Get FullIdentity combined into Uplink

* Structure libuplink a little better

* Update some types and add some comments

* WIP uplink stuff

* Get uplink types and configs figured out

* add initial setup for tests, happy path is working

* Remove dependency from miniogw

* Adds miniogw code and wires it up correctly

* WIP working on getting test suite setup

* Uplink client now returns successfully and passes some initial happy path tets

* WIP trying to get v2 draft ready

* WIP

* WIP wiring up bucket methods and adjusting to some review feedback

* Getting closer to v2 libuplink draft

* CreateBucket now works and has tests to prove it

* Bucket tests are passing now

* removing some code

* Updates error handling and linter fixes

* Removes main_test

* Uploads and downloads are now working

* Rename BucketOpts to Encryption

* updates

* added test file back to git that was being ignored for some reason

* more test conditions

* changes Checksum in ObjectMeta struct to be type []byte

* linter fix

* Updates how encryption is passed through to bucket opts

* Updates encryption handling at bucket and access level

* Fixes imports

* Updates per code review
2019-03-20 09:43:53 -06:00

92 lines
2.5 KiB
Go

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package uplink
import (
"context"
"storj.io/storj/pkg/storj"
)
// Access holds a reference to Uplink and a set of permissions for actions on a bucket.
type Access struct {
Permissions Permissions
Uplink *Uplink
}
// A Macaroon represents an access credential to certain resources
type Macaroon interface {
Serialize() ([]byte, error)
Restrict(caveats ...Caveat) Macaroon
}
// Permissions are parsed by Uplink and return an Access struct
type Permissions struct {
Macaroon Macaroon
APIKey string
}
// Caveat could be a read-only restriction, a time-bound
// restriction, a bucket-specific restriction, a path-prefix restriction, a
// full path restriction, etc.
type Caveat interface {
}
// CreateBucketOptions holds the bucket opts
type CreateBucketOptions struct {
Encryption Encryption
}
// CreateBucket creates a bucket from the passed opts
func (a *Access) CreateBucket(ctx context.Context, bucket string, opts CreateBucketOptions) (storj.Bucket, error) {
metainfo, _, err := a.Uplink.config.GetMetainfo(ctx, a.Uplink.id)
if err != nil {
return storj.Bucket{}, Error.Wrap(err)
}
return metainfo.CreateBucket(ctx, bucket, &storj.Bucket{PathCipher: opts.Encryption.PathCipher})
}
// DeleteBucket deletes a bucket if authorized
func (a *Access) DeleteBucket(ctx context.Context, bucket string) error {
metainfo, _, err := a.Uplink.config.GetMetainfo(ctx, a.Uplink.id)
if err != nil {
return Error.Wrap(err)
}
return metainfo.DeleteBucket(ctx, bucket)
}
// ListBuckets will list authorized buckets
func (a *Access) ListBuckets(ctx context.Context, opts storj.BucketListOptions) (storj.BucketList, error) {
metainfo, _, err := a.Uplink.config.GetMetainfo(ctx, a.Uplink.id)
if err != nil {
return storj.BucketList{}, Error.Wrap(err)
}
return metainfo.ListBuckets(ctx, opts)
}
// GetBucketInfo returns info about the requested bucket if authorized
func (a *Access) GetBucketInfo(ctx context.Context, bucket string) (storj.Bucket, error) {
metainfo, _, err := a.Uplink.config.GetMetainfo(ctx, a.Uplink.id)
if err != nil {
return storj.Bucket{}, Error.Wrap(err)
}
return metainfo.GetBucket(ctx, bucket)
}
// GetBucket returns a Bucket with the given Encryption information
func (a *Access) GetBucket(ctx context.Context, bucket string, encryption *Encryption) *Bucket {
return &Bucket{
Access: a,
Enc: encryption,
Bucket: storj.Bucket{
Name: bucket,
PathCipher: encryption.PathCipher,
},
}
}