[#25] Migrate auth package to NeoFS API v2
- upgrade to NeoFS API v2 - upgrade to new Authmate package closes #25 Signed-off-by: Evgeniy Kulikov <kim@nspcc.ru>
This commit is contained in:
parent
b346a77933
commit
3b1fd968f8
2 changed files with 42 additions and 81 deletions
|
@ -3,7 +3,7 @@ package auth
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
"github.com/nspcc-dev/neofs-api-go/service"
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -12,12 +12,12 @@ type contextKey string
|
||||||
const bearerTokenContextKey contextKey = "bearer-token"
|
const bearerTokenContextKey contextKey = "bearer-token"
|
||||||
|
|
||||||
// GetBearerToken returns a bearer token embedded into a context or error, if any.
|
// GetBearerToken returns a bearer token embedded into a context or error, if any.
|
||||||
func GetBearerToken(ctx context.Context) (*service.BearerTokenMsg, error) {
|
func GetBearerToken(ctx context.Context) (*token.BearerToken, error) {
|
||||||
bt := ctx.Value(bearerTokenContextKey)
|
bt := ctx.Value(bearerTokenContextKey)
|
||||||
if bt == nil {
|
if bt == nil {
|
||||||
return nil, errors.New("got nil bearer token")
|
return nil, errors.New("got nil bearer token")
|
||||||
}
|
}
|
||||||
v, ok := bt.(*service.BearerTokenMsg)
|
v, ok := bt.(*token.BearerToken)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.Errorf("extracted unexpected type other than bearer token's: %T", v)
|
return nil, errors.Errorf("extracted unexpected type other than bearer token's: %T", v)
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,6 @@ func GetBearerToken(ctx context.Context) (*service.BearerTokenMsg, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetBearerToken return a context with embedded bearer token.
|
// SetBearerToken return a context with embedded bearer token.
|
||||||
func SetBearerToken(ctx context.Context, bearerToken *service.BearerTokenMsg) context.Context {
|
func SetBearerToken(ctx context.Context, bearerToken *token.BearerToken) context.Context {
|
||||||
return context.WithValue(ctx, bearerTokenContextKey, bearerToken)
|
return context.WithValue(ctx, bearerTokenContextKey, bearerToken)
|
||||||
}
|
}
|
||||||
|
|
115
auth/center.go
115
auth/center.go
|
@ -11,71 +11,56 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
aws_credentials "github.com/aws/aws-sdk-go/aws/credentials"
|
"github.com/aws/aws-sdk-go/aws/credentials"
|
||||||
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
|
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
|
||||||
"github.com/nspcc-dev/neofs-api-go/refs"
|
"github.com/nspcc-dev/neofs-api-go/pkg/token"
|
||||||
"github.com/nspcc-dev/neofs-api-go/service"
|
|
||||||
"github.com/nspcc-dev/neofs-authmate/accessbox/hcs"
|
"github.com/nspcc-dev/neofs-authmate/accessbox/hcs"
|
||||||
"github.com/nspcc-dev/neofs-authmate/credentials"
|
"github.com/nspcc-dev/neofs-authmate/agents/s3"
|
||||||
"github.com/nspcc-dev/neofs-authmate/gates"
|
manager "github.com/nspcc-dev/neofs-authmate/manager/neofs"
|
||||||
manager "github.com/nspcc-dev/neofs-authmate/neofsmanager"
|
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
)
|
)
|
||||||
|
|
||||||
var authorizationFieldRegexp = regexp.MustCompile(`AWS4-HMAC-SHA256 Credential=(?P<access_key_id_cid>[^/]+)/(?P<access_key_id_oid>[^/]+)/(?P<date>[^/]+)/(?P<region>[^/]*)/(?P<service>[^/]+)/aws4_request,\s*SignedHeaders=(?P<signed_header_fields>.+),\s*Signature=(?P<v4_signature>.+)`)
|
var authorizationFieldRegexp = regexp.MustCompile(`AWS4-HMAC-SHA256 Credential=(?P<access_key_id_cid>[^/]+)/(?P<access_key_id_oid>[^/]+)/(?P<date>[^/]+)/(?P<region>[^/]*)/(?P<service>[^/]+)/aws4_request,\s*SignedHeaders=(?P<signed_header_fields>.+),\s*Signature=(?P<v4_signature>.+)`)
|
||||||
|
|
||||||
const emptyStringSHA256 = `e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855`
|
type (
|
||||||
|
Center struct {
|
||||||
|
man *manager.Manager
|
||||||
|
reg *regexpSubmatcher
|
||||||
|
|
||||||
// Center is a central app's authentication/authorization management unit.
|
keys *hcs.X25519Keys
|
||||||
type Center struct {
|
}
|
||||||
log *zap.Logger
|
|
||||||
submatcher *regexpSubmatcher
|
Params struct {
|
||||||
neofsCredentials *credentials.Credentials
|
Timeout time.Duration
|
||||||
manager *manager.Manager
|
|
||||||
authKeys *hcs.X25519Keys
|
Log *zap.Logger
|
||||||
}
|
Con manager.Connector
|
||||||
|
|
||||||
|
GAKey *hcs.X25519Keys
|
||||||
|
NFKey *ecdsa.PrivateKey
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
// New creates an instance of AuthCenter.
|
||||||
|
func New(ctx context.Context, p *Params) (*Center, error) {
|
||||||
|
m, err := manager.New(ctx,
|
||||||
|
manager.WithKey(p.NFKey),
|
||||||
|
manager.WithLogger(p.Log),
|
||||||
|
manager.WithConnector(p.Con))
|
||||||
|
|
||||||
// NewCenter creates an instance of AuthCenter.
|
|
||||||
func NewCenter(log *zap.Logger, neofsNodeAddress string) (*Center, error) {
|
|
||||||
m, err := manager.NewManager(neofsNodeAddress)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &Center{
|
return &Center{
|
||||||
log: log,
|
man: m,
|
||||||
submatcher: ®expSubmatcher{re: authorizationFieldRegexp},
|
reg: ®expSubmatcher{re: authorizationFieldRegexp},
|
||||||
manager: m,
|
|
||||||
|
keys: p.GAKey,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (center *Center) SetNeoFSKeys(key *ecdsa.PrivateKey) error {
|
func (center *Center) AuthenticationPassed(request *http.Request) (*token.BearerToken, error) {
|
||||||
creds, err := credentials.NewFromKey(key)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
center.neofsCredentials = creds
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (center *Center) GetNeoFSPrivateKey() *ecdsa.PrivateKey {
|
|
||||||
return center.neofsCredentials.Key()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (center *Center) GetOwnerID() refs.OwnerID {
|
|
||||||
return center.neofsCredentials.OwnerID()
|
|
||||||
}
|
|
||||||
|
|
||||||
func (center *Center) SetAuthKeys(key hcs.X25519PrivateKey) error {
|
|
||||||
keys, err := hcs.NewKeys(key)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
center.authKeys = keys
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (center *Center) AuthenticationPassed(request *http.Request) (*service.BearerTokenMsg, error) {
|
|
||||||
queryValues := request.URL.Query()
|
queryValues := request.URL.Query()
|
||||||
if queryValues.Get("X-Amz-Algorithm") == "AWS4-HMAC-SHA256" {
|
if queryValues.Get("X-Amz-Algorithm") == "AWS4-HMAC-SHA256" {
|
||||||
return nil, errors.New("pre-signed form of request is not supported")
|
return nil, errors.New("pre-signed form of request is not supported")
|
||||||
|
@ -84,7 +69,7 @@ func (center *Center) AuthenticationPassed(request *http.Request) (*service.Bear
|
||||||
if len(authHeaderField) != 1 {
|
if len(authHeaderField) != 1 {
|
||||||
return nil, errors.New("unsupported request: wrong length of Authorization header field")
|
return nil, errors.New("unsupported request: wrong length of Authorization header field")
|
||||||
}
|
}
|
||||||
sms1 := center.submatcher.getSubmatches(authHeaderField[0])
|
sms1 := center.reg.getSubmatches(authHeaderField[0])
|
||||||
if len(sms1) != 7 {
|
if len(sms1) != 7 {
|
||||||
return nil, errors.New("bad Authorization header field")
|
return nil, errors.New("bad Authorization header field")
|
||||||
}
|
}
|
||||||
|
@ -97,7 +82,7 @@ func (center *Center) AuthenticationPassed(request *http.Request) (*service.Bear
|
||||||
return nil, errors.Wrap(err, "failed to parse x-amz-date header field")
|
return nil, errors.Wrap(err, "failed to parse x-amz-date header field")
|
||||||
}
|
}
|
||||||
accessKeyID := fmt.Sprintf("%s/%s", sms1["access_key_id_cid"], sms1["access_key_id_oid"])
|
accessKeyID := fmt.Sprintf("%s/%s", sms1["access_key_id_cid"], sms1["access_key_id_oid"])
|
||||||
bearerToken, secretAccessKey, err := center.fetchBearerToken(accessKeyID)
|
res, err := s3.NewAgent(center.man).ObtainSecret(request.Context(), center.keys, accessKeyID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to fetch bearer token")
|
return nil, errors.Wrap(err, "failed to fetch bearer token")
|
||||||
}
|
}
|
||||||
|
@ -110,7 +95,7 @@ func (center *Center) AuthenticationPassed(request *http.Request) (*service.Bear
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
awsCreds := aws_credentials.NewStaticCredentials(accessKeyID, secretAccessKey, "")
|
awsCreds := credentials.NewStaticCredentials(accessKeyID, res.SecretAccessKey, "")
|
||||||
signer := v4.NewSigner(awsCreds)
|
signer := v4.NewSigner(awsCreds)
|
||||||
body, err := readAndKeepBody(request)
|
body, err := readAndKeepBody(request)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -120,31 +105,11 @@ func (center *Center) AuthenticationPassed(request *http.Request) (*service.Bear
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "failed to sign temporary HTTP request")
|
return nil, errors.Wrap(err, "failed to sign temporary HTTP request")
|
||||||
}
|
}
|
||||||
sms2 := center.submatcher.getSubmatches(otherRequest.Header.Get("Authorization"))
|
sms2 := center.reg.getSubmatches(otherRequest.Header.Get("Authorization"))
|
||||||
if sms1["v4_signature"] != sms2["v4_signature"] {
|
if sms1["v4_signature"] != sms2["v4_signature"] {
|
||||||
return nil, errors.Wrap(err, "failed to pass authentication procedure")
|
return nil, errors.Wrap(err, "failed to pass authentication procedure")
|
||||||
}
|
}
|
||||||
return bearerToken, nil
|
return res.BearerToken, nil
|
||||||
}
|
|
||||||
|
|
||||||
func (center *Center) fetchBearerToken(accessKeyID string) (*service.BearerTokenMsg, string, error) {
|
|
||||||
akid := new(refs.Address)
|
|
||||||
if err := akid.Parse(accessKeyID); err != nil {
|
|
||||||
return nil, "", errors.Wrap(err, "failed to parse access key id as refs.Address")
|
|
||||||
}
|
|
||||||
config := &gates.ObtainingConfig{
|
|
||||||
BaseConfig: gates.BaseConfig{
|
|
||||||
OperationalCredentials: center.neofsCredentials,
|
|
||||||
Manager: center.manager,
|
|
||||||
},
|
|
||||||
GateKeys: center.authKeys,
|
|
||||||
SecretAddress: akid,
|
|
||||||
}
|
|
||||||
res, err := gates.ObtainSecret(config)
|
|
||||||
if err != nil {
|
|
||||||
return nil, "", errors.Wrap(err, "failed to obtain secret")
|
|
||||||
}
|
|
||||||
return res.BearerToken, res.SecretAccessKey, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Make this write into a smart buffer backed by a file on a fast drive.
|
// TODO: Make this write into a smart buffer backed by a file on a fast drive.
|
||||||
|
@ -160,7 +125,3 @@ func readAndKeepBody(request *http.Request) (*bytes.Reader, error) {
|
||||||
request.Body = ioutil.NopCloser(bytes.NewReader(payload))
|
request.Body = ioutil.NopCloser(bytes.NewReader(payload))
|
||||||
return bytes.NewReader(payload), nil
|
return bytes.NewReader(payload), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoadGateAuthPrivateKey(path string) (hcs.X25519PrivateKey, error) {
|
|
||||||
return ioutil.ReadFile(path)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue