2021-11-09 08:07:49 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-05-30 08:40:28 +00:00
|
|
|
"crypto/ecdsa"
|
2023-04-14 13:35:33 +00:00
|
|
|
"fmt"
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2024-10-07 14:20:25 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/refs"
|
|
|
|
rpcapi "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/rpc/client"
|
|
|
|
v2session "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/session"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/api/signature"
|
2023-04-14 13:35:33 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
2023-03-07 11:20:03 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2021-11-09 08:07:49 +00:00
|
|
|
)
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// PrmSessionCreate groups parameters of SessionCreate operation.
|
|
|
|
type PrmSessionCreate struct {
|
2023-09-05 14:30:42 +00:00
|
|
|
XHeaders []string
|
2022-03-03 11:04:53 +00:00
|
|
|
|
2023-09-05 14:30:42 +00:00
|
|
|
Expiration uint64
|
2022-05-30 08:40:28 +00:00
|
|
|
|
2023-09-05 14:30:42 +00:00
|
|
|
Key *ecdsa.PrivateKey
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetExp sets number of the last NepFS epoch in the lifetime of the session after which it will be expired.
|
2023-09-05 14:30:42 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmSessionCreate.Expiration instead.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x *PrmSessionCreate) SetExp(exp uint64) {
|
2023-09-05 14:30:42 +00:00
|
|
|
x.Expiration = exp
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2022-05-30 08:40:28 +00:00
|
|
|
// UseKey specifies private key to sign the requests and compute token owner.
|
|
|
|
// If key is not provided, then Client default key is used.
|
2023-09-05 14:30:42 +00:00
|
|
|
//
|
|
|
|
// Deprecated: Use PrmSessionCreate.Key instead.
|
2022-05-30 08:40:28 +00:00
|
|
|
func (x *PrmSessionCreate) UseKey(key ecdsa.PrivateKey) {
|
2023-09-05 14:30:42 +00:00
|
|
|
x.Key = &key
|
2022-05-30 08:40:28 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 13:35:33 +00:00
|
|
|
func (x *PrmSessionCreate) buildRequest(c *Client) (*v2session.CreateRequest, error) {
|
2023-11-13 12:18:07 +00:00
|
|
|
ownerKey := c.prm.Key.PublicKey
|
2023-09-05 14:30:42 +00:00
|
|
|
if x.Key != nil {
|
|
|
|
ownerKey = x.Key.PublicKey
|
2023-04-14 13:35:33 +00:00
|
|
|
}
|
|
|
|
var ownerID user.ID
|
|
|
|
user.IDFromKey(&ownerID, ownerKey)
|
|
|
|
|
|
|
|
var ownerIDV2 refs.OwnerID
|
|
|
|
ownerID.WriteToV2(&ownerIDV2)
|
|
|
|
|
|
|
|
reqBody := new(v2session.CreateRequestBody)
|
|
|
|
reqBody.SetOwnerID(&ownerIDV2)
|
2023-09-05 14:30:42 +00:00
|
|
|
reqBody.SetExpiration(x.Expiration)
|
2023-04-14 13:35:33 +00:00
|
|
|
|
|
|
|
var meta v2session.RequestMetaHeader
|
2023-09-05 14:30:42 +00:00
|
|
|
writeXHeadersToMeta(x.XHeaders, &meta)
|
2023-04-14 13:35:33 +00:00
|
|
|
|
|
|
|
var req v2session.CreateRequest
|
|
|
|
req.SetBody(reqBody)
|
|
|
|
c.prepareRequest(&req, &meta)
|
|
|
|
return &req, nil
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// ResSessionCreate groups resulting values of SessionCreate operation.
|
|
|
|
type ResSessionCreate struct {
|
2021-11-16 18:17:25 +00:00
|
|
|
statusRes
|
|
|
|
|
|
|
|
id []byte
|
|
|
|
|
|
|
|
sessionKey []byte
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// ID returns identifier of the opened session in a binary FrostFS API protocol format.
|
2021-12-02 12:21:54 +00:00
|
|
|
//
|
|
|
|
// Client doesn't retain value so modification is safe.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x ResSessionCreate) ID() []byte {
|
2021-11-16 18:17:25 +00:00
|
|
|
return x.id
|
|
|
|
}
|
|
|
|
|
2022-12-29 10:46:18 +00:00
|
|
|
// PublicKey returns public key of the opened session in a binary FrostFS API protocol format.
|
2022-02-17 16:10:49 +00:00
|
|
|
func (x ResSessionCreate) PublicKey() []byte {
|
2021-11-16 18:17:25 +00:00
|
|
|
return x.sessionKey
|
|
|
|
}
|
|
|
|
|
2022-02-17 16:10:49 +00:00
|
|
|
// SessionCreate opens a session with the node server on the remote endpoint.
|
2021-12-02 12:21:54 +00:00
|
|
|
// The session lifetime coincides with the server lifetime. Results can be written
|
|
|
|
// to session token which can be later attached to the requests.
|
2021-12-29 13:55:29 +00:00
|
|
|
//
|
2022-01-24 10:33:43 +00:00
|
|
|
// Exactly one return value is non-nil. By default, server status is returned in res structure.
|
2021-12-02 12:21:54 +00:00
|
|
|
// Any client's internal or transport errors are returned as `error`.
|
2023-09-15 15:52:08 +00:00
|
|
|
// If PrmInit.DisableFrostFSFailuresResolution has been called, unsuccessful
|
2023-09-12 15:57:04 +00:00
|
|
|
// FrostFS status codes are included in the returned result structure,
|
|
|
|
// otherwise, are also returned as `error`.
|
2021-12-02 12:21:54 +00:00
|
|
|
//
|
2023-02-27 07:53:27 +00:00
|
|
|
// Returns an error if parameters are set incorrectly (see PrmSessionCreate docs).
|
2021-12-02 12:21:54 +00:00
|
|
|
// Context is required and must not be nil. It is used for network communication.
|
|
|
|
//
|
2022-01-24 10:33:43 +00:00
|
|
|
// Return statuses:
|
2022-08-24 14:17:40 +00:00
|
|
|
// - global (see Client docs).
|
2022-02-17 16:10:49 +00:00
|
|
|
func (c *Client) SessionCreate(ctx context.Context, prm PrmSessionCreate) (*ResSessionCreate, error) {
|
2023-04-14 13:35:33 +00:00
|
|
|
req, err := prm.buildRequest(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-05-30 08:40:28 +00:00
|
|
|
}
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2023-11-13 12:18:07 +00:00
|
|
|
if err := signature.SignServiceMessage(&c.prm.Key, req); err != nil {
|
2023-04-14 13:35:33 +00:00
|
|
|
return nil, fmt.Errorf("sign request: %w", err)
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
2021-11-16 18:17:25 +00:00
|
|
|
|
2023-04-14 13:35:33 +00:00
|
|
|
resp, err := rpcapi.CreateSession(&c.c, req, client.WithContext(ctx))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|
|
|
|
|
2023-04-14 13:35:33 +00:00
|
|
|
var res ResSessionCreate
|
|
|
|
res.st, err = c.processResponse(resp)
|
|
|
|
if err != nil || !apistatus.IsSuccessful(res.st) {
|
|
|
|
return &res, err
|
2021-12-02 12:21:54 +00:00
|
|
|
}
|
2021-11-09 08:07:49 +00:00
|
|
|
|
2023-04-14 13:35:33 +00:00
|
|
|
body := resp.GetBody()
|
|
|
|
res.id = body.GetID()
|
|
|
|
res.sessionKey = body.GetSessionKey()
|
2021-12-02 12:21:54 +00:00
|
|
|
return &res, nil
|
2021-11-09 08:07:49 +00:00
|
|
|
}
|