2021-05-19 09:28:17 +00:00
|
|
|
package authmate
|
2021-05-18 18:49:09 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2021-10-26 12:03:51 +00:00
|
|
|
"encoding/binary"
|
2021-05-25 16:52:29 +00:00
|
|
|
"encoding/hex"
|
2021-05-18 18:49:09 +00:00
|
|
|
"encoding/json"
|
2021-05-20 10:14:17 +00:00
|
|
|
"fmt"
|
2021-05-18 18:49:09 +00:00
|
|
|
"io"
|
|
|
|
"math"
|
2021-09-01 11:30:15 +00:00
|
|
|
"os"
|
2021-05-18 18:49:09 +00:00
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
|
2021-06-16 20:09:51 +00:00
|
|
|
"github.com/google/uuid"
|
2021-06-24 15:21:34 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2021-09-10 06:56:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/api/cache"
|
2021-06-14 13:39:25 +00:00
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/accessbox"
|
|
|
|
"github.com/nspcc-dev/neofs-s3-gw/creds/tokens"
|
2021-11-15 12:56:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
|
|
|
cid "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/eacl"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/owner"
|
2021-08-27 12:48:59 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/policy"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/pool"
|
2021-11-15 12:56:16 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/token"
|
2021-05-18 18:49:09 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2021-05-26 16:48:27 +00:00
|
|
|
const (
|
2021-06-17 12:38:07 +00:00
|
|
|
defaultAuthContainerBasicACL uint32 = 0b00111100100011001000110011001110
|
2021-05-26 16:48:27 +00:00
|
|
|
)
|
2021-05-18 18:49:09 +00:00
|
|
|
|
2021-05-19 16:27:02 +00:00
|
|
|
// Agent contains client communicating with NeoFS and logger.
|
2021-05-18 18:49:09 +00:00
|
|
|
type Agent struct {
|
2021-05-28 20:48:23 +00:00
|
|
|
pool pool.Pool
|
|
|
|
log *zap.Logger
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:27:02 +00:00
|
|
|
// New creates an object of type Agent that consists of Client and logger.
|
2021-05-28 20:48:23 +00:00
|
|
|
func New(log *zap.Logger, conns pool.Pool) *Agent {
|
|
|
|
return &Agent{log: log, pool: conns}
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type (
|
2021-07-16 12:35:07 +00:00
|
|
|
// ContainerPolicies contains mapping of aws LocationConstraint to neofs PlacementPolicy.
|
|
|
|
ContainerPolicies map[string]string
|
|
|
|
|
2021-05-19 16:27:02 +00:00
|
|
|
// IssueSecretOptions contains options for passing to Agent.IssueSecret method.
|
2021-05-18 18:49:09 +00:00
|
|
|
IssueSecretOptions struct {
|
2021-06-04 13:01:42 +00:00
|
|
|
ContainerID *cid.ID
|
2021-05-18 18:49:09 +00:00
|
|
|
ContainerFriendlyName string
|
2021-06-24 15:21:34 +00:00
|
|
|
NeoFSKey *keys.PrivateKey
|
|
|
|
GatesPublicKeys []*keys.PublicKey
|
2021-05-18 18:49:09 +00:00
|
|
|
EACLRules []byte
|
2021-06-16 20:09:51 +00:00
|
|
|
ContextRules []byte
|
|
|
|
SessionTkn bool
|
2021-10-26 12:03:51 +00:00
|
|
|
Lifetime time.Duration
|
2021-09-01 11:30:15 +00:00
|
|
|
AwsCliCredentialsFile string
|
2021-07-16 12:35:07 +00:00
|
|
|
ContainerPolicies ContainerPolicies
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:27:02 +00:00
|
|
|
// ObtainSecretOptions contains options for passing to Agent.ObtainSecret method.
|
2021-05-18 18:49:09 +00:00
|
|
|
ObtainSecretOptions struct {
|
|
|
|
SecretAddress string
|
2021-06-24 15:21:34 +00:00
|
|
|
GatePrivateKey *keys.PrivateKey
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-06-28 13:20:11 +00:00
|
|
|
// lifetimeOptions holds NeoFS epochs, iat -- epoch, which a token was issued at, exp -- epoch, when the token expires.
|
|
|
|
type lifetimeOptions struct {
|
|
|
|
Iat uint64
|
|
|
|
Exp uint64
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:03:51 +00:00
|
|
|
type epochDurations struct {
|
|
|
|
currentEpoch uint64
|
|
|
|
msPerBlock int64
|
|
|
|
blocksInEpoch uint64
|
|
|
|
}
|
|
|
|
|
2021-05-18 18:49:09 +00:00
|
|
|
type (
|
|
|
|
issuingResult struct {
|
|
|
|
AccessKeyID string `json:"access_key_id"`
|
|
|
|
SecretAccessKey string `json:"secret_access_key"`
|
|
|
|
OwnerPrivateKey string `json:"owner_private_key"`
|
2021-07-16 12:08:05 +00:00
|
|
|
ContainerID string `json:"container_id"`
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obtainingResult struct {
|
|
|
|
BearerToken *token.BearerToken `json:"-"`
|
|
|
|
SecretAccessKey string `json:"secret_access_key"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-06-04 13:01:42 +00:00
|
|
|
func (a *Agent) checkContainer(ctx context.Context, cid *cid.ID, friendlyName string) (*cid.ID, error) {
|
2021-05-18 18:49:09 +00:00
|
|
|
if cid != nil {
|
|
|
|
// check that container exists
|
2021-06-23 20:26:48 +00:00
|
|
|
_, err := a.pool.GetContainer(ctx, cid)
|
2021-05-18 18:49:09 +00:00
|
|
|
return cid, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pp, err := buildPlacementPolicy("")
|
|
|
|
if err != nil {
|
2021-05-20 10:14:17 +00:00
|
|
|
return nil, fmt.Errorf("failed to build placement policy: %w", err)
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cnr := container.New(
|
|
|
|
container.WithPolicy(pp),
|
|
|
|
container.WithCustomBasicACL(defaultAuthContainerBasicACL),
|
|
|
|
container.WithAttribute(container.AttributeName, friendlyName),
|
|
|
|
container.WithAttribute(container.AttributeTimestamp, strconv.FormatInt(time.Now().Unix(), 10)))
|
|
|
|
|
2021-06-23 20:26:48 +00:00
|
|
|
cid, err = a.pool.PutContainer(ctx, cnr)
|
2021-05-26 16:48:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-23 20:26:48 +00:00
|
|
|
if err := a.pool.WaitForContainerPresence(ctx, cid, pool.DefaultPollingParams()); err != nil {
|
|
|
|
return nil, err
|
2021-05-26 16:48:27 +00:00
|
|
|
}
|
2021-06-23 20:26:48 +00:00
|
|
|
return cid, nil
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 12:03:51 +00:00
|
|
|
func (a *Agent) getEpochDurations(ctx context.Context) (*epochDurations, error) {
|
2021-06-28 13:20:11 +00:00
|
|
|
if conn, _, err := a.pool.Connection(); err != nil {
|
2021-10-26 12:03:51 +00:00
|
|
|
return nil, err
|
2021-06-28 13:20:11 +00:00
|
|
|
} else if networkInfo, err := conn.NetworkInfo(ctx); err != nil {
|
2021-10-26 12:03:51 +00:00
|
|
|
return nil, err
|
2021-06-28 13:20:11 +00:00
|
|
|
} else {
|
2021-10-26 12:03:51 +00:00
|
|
|
res := &epochDurations{
|
|
|
|
currentEpoch: networkInfo.CurrentEpoch(),
|
|
|
|
msPerBlock: networkInfo.MsPerBlock(),
|
|
|
|
}
|
|
|
|
|
|
|
|
networkInfo.NetworkConfig().IterateParameters(func(parameter *netmap.NetworkParameter) bool {
|
|
|
|
if string(parameter.Key()) == "EpochDuration" {
|
|
|
|
data := make([]byte, 8)
|
|
|
|
copy(data, parameter.Value())
|
|
|
|
res.blocksInEpoch = binary.LittleEndian.Uint64(data)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
if res.blocksInEpoch == 0 {
|
|
|
|
return nil, fmt.Errorf("not found param: EpochDuration")
|
|
|
|
}
|
|
|
|
return res, nil
|
2021-06-28 13:20:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
func checkPolicy(policyString string) (*netmap.PlacementPolicy, error) {
|
|
|
|
result, err := policy.Parse(policyString)
|
|
|
|
if err == nil {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result = netmap.NewPlacementPolicy()
|
|
|
|
if err = result.UnmarshalJSON([]byte(policyString)); err == nil {
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("can't parse placement policy")
|
|
|
|
}
|
|
|
|
|
|
|
|
func preparePolicy(policy ContainerPolicies) ([]*accessbox.AccessBox_ContainerPolicy, error) {
|
|
|
|
if policy == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []*accessbox.AccessBox_ContainerPolicy
|
|
|
|
for locationConstraint, placementPolicy := range policy {
|
|
|
|
parsedPolicy, err := checkPolicy(placementPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
marshaled, err := parsedPolicy.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("can't marshal placement policy: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
result = append(result, &accessbox.AccessBox_ContainerPolicy{
|
|
|
|
LocationConstraint: locationConstraint,
|
|
|
|
Policy: marshaled,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:27:02 +00:00
|
|
|
// IssueSecret creates an auth token, puts it in the NeoFS network and writes to io.Writer a new secret access key.
|
2021-05-18 18:49:09 +00:00
|
|
|
func (a *Agent) IssueSecret(ctx context.Context, w io.Writer, options *IssueSecretOptions) error {
|
|
|
|
var (
|
2021-06-28 13:20:11 +00:00
|
|
|
err error
|
|
|
|
cid *cid.ID
|
|
|
|
box *accessbox.AccessBox
|
|
|
|
lifetime lifetimeOptions
|
2021-05-18 18:49:09 +00:00
|
|
|
)
|
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
policies, err := preparePolicy(options.ContainerPolicies)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:03:51 +00:00
|
|
|
durations, err := a.getEpochDurations(ctx)
|
2021-06-28 13:20:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-10-26 12:03:51 +00:00
|
|
|
lifetime.Iat = durations.currentEpoch
|
|
|
|
msPerEpoch := durations.blocksInEpoch * uint64(durations.msPerBlock)
|
|
|
|
epochLifetime := uint64(options.Lifetime.Milliseconds()) / msPerEpoch
|
|
|
|
if uint64(options.Lifetime.Milliseconds())%msPerEpoch != 0 {
|
|
|
|
epochLifetime++
|
|
|
|
}
|
2021-06-28 13:20:11 +00:00
|
|
|
|
2021-10-26 12:03:51 +00:00
|
|
|
if epochLifetime >= math.MaxUint64-lifetime.Iat {
|
2021-06-28 13:20:11 +00:00
|
|
|
lifetime.Exp = math.MaxUint64
|
|
|
|
} else {
|
2021-10-26 12:03:51 +00:00
|
|
|
lifetime.Exp = lifetime.Iat + epochLifetime
|
2021-06-28 13:20:11 +00:00
|
|
|
}
|
|
|
|
|
2021-05-18 18:49:09 +00:00
|
|
|
a.log.Info("check container", zap.Stringer("cid", options.ContainerID))
|
|
|
|
if cid, err = a.checkContainer(ctx, options.ContainerID, options.ContainerFriendlyName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-06-28 13:20:11 +00:00
|
|
|
gatesData, err := createTokens(options, lifetime, cid)
|
2021-05-18 18:49:09 +00:00
|
|
|
if err != nil {
|
2021-05-20 10:14:17 +00:00
|
|
|
return fmt.Errorf("failed to build bearer token: %w", err)
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
2021-06-18 15:15:58 +00:00
|
|
|
box, secrets, err := accessbox.PackTokens(gatesData)
|
2021-06-14 13:39:25 +00:00
|
|
|
if err != nil {
|
2021-06-18 15:15:58 +00:00
|
|
|
return err
|
2021-06-14 13:39:25 +00:00
|
|
|
}
|
2021-06-16 20:09:51 +00:00
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
box.ContainerPolicy = policies
|
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
oid, err := OwnerIDFromNeoFSKey(options.NeoFSKey.PublicKey())
|
2021-06-16 14:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-06-16 20:09:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
a.log.Info("store bearer token into NeoFS",
|
2021-06-17 16:45:50 +00:00
|
|
|
zap.Stringer("owner_tkn", oid))
|
2021-06-16 14:07:31 +00:00
|
|
|
|
2021-06-16 20:09:51 +00:00
|
|
|
if !options.SessionTkn && len(options.ContextRules) > 0 {
|
|
|
|
_, err := w.Write([]byte("Warning: rules for session token were set but --create-session flag wasn't, " +
|
|
|
|
"so session token was not created\n"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2021-05-18 18:49:09 +00:00
|
|
|
|
2021-06-14 13:39:25 +00:00
|
|
|
address, err := tokens.
|
2021-09-10 06:56:56 +00:00
|
|
|
New(a.pool, secrets.EphemeralKey, cache.DefaultAccessBoxConfig()).
|
2021-09-10 08:07:26 +00:00
|
|
|
Put(ctx, cid, oid, box, lifetime.Exp, options.GatesPublicKeys...)
|
2021-05-18 18:49:09 +00:00
|
|
|
if err != nil {
|
2021-05-20 10:14:17 +00:00
|
|
|
return fmt.Errorf("failed to put bearer token: %w", err)
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 11:16:11 +00:00
|
|
|
accessKeyID := address.ContainerID().String() + "0" + address.ObjectID().String()
|
2021-06-02 18:53:20 +00:00
|
|
|
|
2021-05-18 18:49:09 +00:00
|
|
|
ir := &issuingResult{
|
2021-06-02 18:53:20 +00:00
|
|
|
AccessKeyID: accessKeyID,
|
2021-06-17 16:45:50 +00:00
|
|
|
SecretAccessKey: secrets.AccessKey,
|
2021-06-24 15:21:34 +00:00
|
|
|
OwnerPrivateKey: hex.EncodeToString(secrets.EphemeralKey.Bytes()),
|
2021-07-16 12:08:05 +00:00
|
|
|
ContainerID: cid.String(),
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetIndent("", " ")
|
2021-09-01 11:30:15 +00:00
|
|
|
if err = enc.Encode(ir); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.AwsCliCredentialsFile != "" {
|
|
|
|
profileName := "authmate_cred_" + address.ObjectID().String()
|
|
|
|
if _, err = os.Stat(options.AwsCliCredentialsFile); os.IsNotExist(err) {
|
|
|
|
profileName = "default"
|
|
|
|
}
|
|
|
|
file, err := os.OpenFile(options.AwsCliCredentialsFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("couldn't open aws cli credentials file: %w", err)
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
if _, err = file.WriteString(fmt.Sprintf("\n[%s]\naws_access_key_id = %s\naws_secret_access_key = %s\n",
|
|
|
|
profileName, accessKeyID, secrets.AccessKey)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 16:27:02 +00:00
|
|
|
// ObtainSecret receives an existing secret access key from NeoFS and
|
|
|
|
// writes to io.Writer the secret access key.
|
2021-05-18 18:49:09 +00:00
|
|
|
func (a *Agent) ObtainSecret(ctx context.Context, w io.Writer, options *ObtainSecretOptions) error {
|
2021-09-10 06:56:56 +00:00
|
|
|
bearerCreds := tokens.New(a.pool, options.GatePrivateKey, cache.DefaultAccessBoxConfig())
|
2021-05-18 18:49:09 +00:00
|
|
|
address := object.NewAddress()
|
|
|
|
if err := address.Parse(options.SecretAddress); err != nil {
|
2021-05-20 10:14:17 +00:00
|
|
|
return fmt.Errorf("failed to parse secret address: %w", err)
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
2021-07-16 12:35:07 +00:00
|
|
|
box, err := bearerCreds.GetBox(ctx, address)
|
2021-05-18 18:49:09 +00:00
|
|
|
if err != nil {
|
2021-06-17 16:45:50 +00:00
|
|
|
return fmt.Errorf("failed to get tokens: %w", err)
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
or := &obtainingResult{
|
2021-07-16 12:35:07 +00:00
|
|
|
BearerToken: box.Gate.BearerToken,
|
|
|
|
SecretAccessKey: box.Gate.AccessKey,
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
return enc.Encode(or)
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildPlacementPolicy(placementRules string) (*netmap.PlacementPolicy, error) {
|
|
|
|
if len(placementRules) != 0 {
|
|
|
|
return policy.Parse(placementRules)
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
REP 1 IN X // place one copy of object
|
|
|
|
CBF 1
|
|
|
|
SELECT 2 From * AS X // in container of two nodes
|
|
|
|
*/
|
|
|
|
pp := new(netmap.PlacementPolicy)
|
|
|
|
pp.SetContainerBackupFactor(1)
|
|
|
|
pp.SetReplicas([]*netmap.Replica{newReplica("X", 1)}...)
|
|
|
|
pp.SetSelectors([]*netmap.Selector{newSimpleSelector("X", 2)}...)
|
|
|
|
|
|
|
|
return pp, nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 16:27:02 +00:00
|
|
|
// selects <count> nodes in container without any additional attributes.
|
2021-05-18 18:49:09 +00:00
|
|
|
func newSimpleSelector(name string, count uint32) (s *netmap.Selector) {
|
|
|
|
s = new(netmap.Selector)
|
|
|
|
s.SetCount(count)
|
|
|
|
s.SetFilter("*")
|
|
|
|
s.SetName(name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func newReplica(name string, count uint32) (r *netmap.Replica) {
|
|
|
|
r = new(netmap.Replica)
|
|
|
|
r.SetCount(count)
|
|
|
|
r.SetSelector(name)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-06-04 13:01:42 +00:00
|
|
|
func buildEACLTable(cid *cid.ID, eaclTable []byte) (*eacl.Table, error) {
|
2021-05-18 18:49:09 +00:00
|
|
|
table := eacl.NewTable()
|
|
|
|
if len(eaclTable) != 0 {
|
|
|
|
return table, table.UnmarshalJSON(eaclTable)
|
|
|
|
}
|
|
|
|
|
|
|
|
record := eacl.NewRecord()
|
|
|
|
record.SetOperation(eacl.OperationGet)
|
|
|
|
record.SetAction(eacl.ActionAllow)
|
|
|
|
// TODO: Change this later.
|
|
|
|
// from := eacl.HeaderFromObject
|
|
|
|
// matcher := eacl.MatchStringEqual
|
|
|
|
// record.AddFilter(from eacl.FilterHeaderType, matcher eacl.Match, name string, value string)
|
|
|
|
eacl.AddFormedTarget(record, eacl.RoleOthers)
|
|
|
|
table.SetCID(cid)
|
|
|
|
table.AddRecord(record)
|
|
|
|
|
|
|
|
return table, nil
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:09:51 +00:00
|
|
|
func buildContext(rules []byte) (*session.ContainerContext, error) {
|
|
|
|
sessionCtx := session.NewContainerContext() // wildcard == true on by default
|
|
|
|
|
|
|
|
if len(rules) != 0 {
|
|
|
|
// cast ToV2 temporary, because there is no method for unmarshalling in ContainerContext in api-go
|
2021-06-30 10:44:25 +00:00
|
|
|
err := sessionCtx.UnmarshalJSON(rules)
|
2021-06-16 20:09:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to read rules for session token: %w", err)
|
|
|
|
}
|
|
|
|
return sessionCtx, nil
|
2021-05-28 20:48:23 +00:00
|
|
|
}
|
2021-06-16 20:09:51 +00:00
|
|
|
sessionCtx.ForPut()
|
|
|
|
sessionCtx.ApplyTo(nil)
|
|
|
|
return sessionCtx, nil
|
|
|
|
}
|
2021-05-28 20:48:23 +00:00
|
|
|
|
2021-06-28 13:20:11 +00:00
|
|
|
func buildBearerToken(key *keys.PrivateKey, table *eacl.Table, lifetime lifetimeOptions, gateKey *keys.PublicKey) (*token.BearerToken, error) {
|
2021-10-19 15:08:07 +00:00
|
|
|
oid, err := OwnerIDFromNeoFSKey(gateKey)
|
2021-06-16 14:07:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-18 18:49:09 +00:00
|
|
|
bearerToken := token.NewBearerToken()
|
|
|
|
bearerToken.SetEACLTable(table)
|
|
|
|
bearerToken.SetOwner(oid)
|
2021-06-28 13:20:11 +00:00
|
|
|
bearerToken.SetLifetime(lifetime.Exp, lifetime.Iat, lifetime.Iat)
|
2021-05-18 18:49:09 +00:00
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
return bearerToken, bearerToken.SignToken(&key.PrivateKey)
|
2021-05-18 18:49:09 +00:00
|
|
|
}
|
2021-05-25 16:52:29 +00:00
|
|
|
|
2021-06-28 13:20:11 +00:00
|
|
|
func buildBearerTokens(key *keys.PrivateKey, table *eacl.Table, lifetime lifetimeOptions, gatesKeys []*keys.PublicKey) ([]*token.BearerToken, error) {
|
2021-06-18 15:15:58 +00:00
|
|
|
bearerTokens := make([]*token.BearerToken, 0, len(gatesKeys))
|
|
|
|
for _, gateKey := range gatesKeys {
|
2021-06-28 13:20:11 +00:00
|
|
|
tkn, err := buildBearerToken(key, table, lifetime, gateKey)
|
2021-06-17 16:45:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-18 15:15:58 +00:00
|
|
|
bearerTokens = append(bearerTokens, tkn)
|
2021-06-17 16:45:50 +00:00
|
|
|
}
|
2021-06-18 15:15:58 +00:00
|
|
|
return bearerTokens, nil
|
2021-06-17 16:45:50 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 13:20:11 +00:00
|
|
|
func buildSessionToken(key *keys.PrivateKey, oid *owner.ID, lifetime lifetimeOptions, ctx *session.ContainerContext, gateKey *keys.PublicKey) (*session.Token, error) {
|
2021-06-16 20:09:51 +00:00
|
|
|
tok := session.NewToken()
|
|
|
|
tok.SetContext(ctx)
|
|
|
|
uid, err := uuid.New().MarshalBinary()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tok.SetID(uid)
|
|
|
|
tok.SetOwnerID(oid)
|
2021-06-24 15:21:34 +00:00
|
|
|
tok.SetSessionKey(gateKey.Bytes())
|
2021-06-16 20:09:51 +00:00
|
|
|
|
2021-06-28 13:20:11 +00:00
|
|
|
tok.SetIat(lifetime.Iat)
|
|
|
|
tok.SetNbf(lifetime.Iat)
|
|
|
|
tok.SetExp(lifetime.Exp)
|
|
|
|
|
2021-06-24 15:21:34 +00:00
|
|
|
return tok, tok.Sign(&key.PrivateKey)
|
2021-06-16 20:09:51 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 13:20:11 +00:00
|
|
|
func buildSessionTokens(key *keys.PrivateKey, oid *owner.ID, lifetime lifetimeOptions, ctx *session.ContainerContext, gatesKeys []*keys.PublicKey) ([]*session.Token, error) {
|
2021-06-18 15:15:58 +00:00
|
|
|
sessionTokens := make([]*session.Token, 0, len(gatesKeys))
|
|
|
|
for _, gateKey := range gatesKeys {
|
2021-06-28 13:20:11 +00:00
|
|
|
tkn, err := buildSessionToken(key, oid, lifetime, ctx, gateKey)
|
2021-06-18 15:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
sessionTokens = append(sessionTokens, tkn)
|
|
|
|
}
|
|
|
|
return sessionTokens, nil
|
|
|
|
}
|
|
|
|
|
2021-06-28 13:20:11 +00:00
|
|
|
func createTokens(options *IssueSecretOptions, lifetime lifetimeOptions, cid *cid.ID) ([]*accessbox.GateData, error) {
|
2021-06-18 15:15:58 +00:00
|
|
|
gates := make([]*accessbox.GateData, len(options.GatesPublicKeys))
|
|
|
|
|
|
|
|
table, err := buildEACLTable(cid, options.EACLRules)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build eacl table: %w", err)
|
|
|
|
}
|
2021-06-28 13:20:11 +00:00
|
|
|
bearerTokens, err := buildBearerTokens(options.NeoFSKey, table, lifetime, options.GatesPublicKeys)
|
2021-06-18 15:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build bearer tokens: %w", err)
|
|
|
|
}
|
|
|
|
for i, gateKey := range options.GatesPublicKeys {
|
|
|
|
gates[i] = accessbox.NewGateData(gateKey, bearerTokens[i])
|
|
|
|
}
|
|
|
|
|
2021-06-16 14:07:31 +00:00
|
|
|
if options.SessionTkn {
|
|
|
|
sessionRules, err := buildContext(options.ContextRules)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to build context for session token: %w", err)
|
|
|
|
}
|
2021-10-19 15:08:07 +00:00
|
|
|
oid, err := OwnerIDFromNeoFSKey(options.NeoFSKey.PublicKey())
|
2021-06-18 15:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-28 13:20:11 +00:00
|
|
|
sessionTokens, err := buildSessionTokens(options.NeoFSKey, oid, lifetime, sessionRules, options.GatesPublicKeys)
|
2021-06-18 15:15:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for i, sessionToken := range sessionTokens {
|
|
|
|
gates[i].SessionToken = sessionToken
|
|
|
|
}
|
2021-06-16 14:07:31 +00:00
|
|
|
}
|
2021-06-18 15:15:58 +00:00
|
|
|
|
|
|
|
return gates, nil
|
2021-06-16 14:07:31 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
func OwnerIDFromNeoFSKey(key *keys.PublicKey) (*owner.ID, error) {
|
2021-06-24 15:21:34 +00:00
|
|
|
wallet, err := owner.NEO3WalletFromPublicKey((*ecdsa.PublicKey)(key))
|
2021-06-16 20:09:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return owner.NewIDFromNeo3Wallet(wallet), nil
|
|
|
|
}
|