2023-06-23 13:12:18 +00:00
package modules
import (
"context"
"fmt"
"os"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/authmate"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/wallet"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var updateSecretCmd = & cobra . Command {
Use : "update-secret" ,
Short : "Update a secret in FrostFS network" ,
Long : ` Creates new access box that will be available for extend list of s3 gates , preserve all timeout from initial credentials .
After using this command you can use initial access - key - id to interact with newly added gates ` ,
Example : ` To extend list of s3 gates that can use existing credentials run :
2024-12-13 15:18:04 +00:00
frostfs - s3 - authmate update - secret -- wallet wallet . json -- peer s01 . frostfs . devenv : 8080 -- gate - wallet s3 - wallet . json \
2023-06-23 13:12:18 +00:00
-- gate - public - key 031 a6c6fbbdf02ca351745fa86b9ba5a9452d785ac4f7fc2b7548ca2a46c4fcf4a \
-- gate - public - key 021 dc56fc6d81d581ae7605a8e00e0e0bab6cbad566a924a527339475a97a8e38e \
-- acces - key - id EC3tyWpTEKfGNS888PFBpwQzZTrnwDXReGjgAxa8Em1h037VoWktUZCAk1LVA5SvVbVd2NHHb2NQm9jhcd5WFU5VD ` ,
RunE : runUpdateSecretCmd ,
}
func initUpdateSecretCmd ( ) {
updateSecretCmd . Flags ( ) . String ( walletFlag , "" , "Path to the wallet that will be owner of the credentials" )
updateSecretCmd . Flags ( ) . String ( addressFlag , "" , "Address of the wallet account" )
updateSecretCmd . Flags ( ) . String ( peerFlag , "" , "Address of a frostfs peer to connect to" )
updateSecretCmd . Flags ( ) . String ( gateWalletFlag , "" , "Path to the s3 gateway wallet to decrypt accessbox" )
updateSecretCmd . Flags ( ) . String ( gateAddressFlag , "" , "Address of the s3 gateway wallet account" )
2024-08-30 12:05:32 +00:00
updateSecretCmd . Flags ( ) . String ( accessKeyIDFlag , "" , "Access key id of s3 credential for which secret must be updatedd" )
updateSecretCmd . Flags ( ) . String ( containerIDFlag , "" , "CID or NNS name of auth container that contains provided credential (must be provided if custom access key id is used)" )
2023-06-23 13:12:18 +00:00
updateSecretCmd . Flags ( ) . StringSlice ( gatePublicKeyFlag , nil , "Public 256r1 key of a gate (use flags repeatedly for multiple gates or separate them by comma)" )
updateSecretCmd . Flags ( ) . Duration ( poolDialTimeoutFlag , defaultPoolDialTimeout , "Timeout for connection to the node in pool to be established" )
updateSecretCmd . Flags ( ) . Duration ( poolHealthcheckTimeoutFlag , defaultPoolHealthcheckTimeout , "Timeout for request to node to decide if it is alive" )
updateSecretCmd . Flags ( ) . Duration ( poolRebalanceIntervalFlag , defaultPoolRebalanceInterval , "Interval for updating nodes health status" )
updateSecretCmd . Flags ( ) . Duration ( poolStreamTimeoutFlag , defaultPoolStreamTimeout , "Timeout for individual operation in streaming RPC" )
2024-01-19 07:00:04 +00:00
updateSecretCmd . Flags ( ) . String ( attributesFlag , "" , "User attributes in form of Key1=Value1,Key2=Value2 (note: you cannot override system attributes)" )
2024-08-30 12:05:32 +00:00
updateSecretCmd . Flags ( ) . String ( rpcEndpointFlag , "" , "NEO node RPC address (must be provided if container-id is nns name)" )
2023-06-23 13:12:18 +00:00
_ = updateSecretCmd . MarkFlagRequired ( walletFlag )
_ = updateSecretCmd . MarkFlagRequired ( peerFlag )
_ = updateSecretCmd . MarkFlagRequired ( gateWalletFlag )
_ = updateSecretCmd . MarkFlagRequired ( accessKeyIDFlag )
_ = updateSecretCmd . MarkFlagRequired ( gatePublicKeyFlag )
}
func runUpdateSecretCmd ( cmd * cobra . Command , _ [ ] string ) error {
ctx , cancel := context . WithTimeout ( cmd . Context ( ) , viper . GetDuration ( timeoutFlag ) )
defer cancel ( )
log := getLogger ( )
password := wallet . GetPassword ( viper . GetViper ( ) , walletPassphraseCfg )
key , err := wallet . GetKeyFromPath ( viper . GetString ( walletFlag ) , viper . GetString ( addressFlag ) , password )
if err != nil {
2023-09-04 18:01:56 +00:00
return wrapPreparationError ( fmt . Errorf ( "failed to load frostfs private key: %s" , err ) )
2023-06-23 13:12:18 +00:00
}
gatePassword := wallet . GetPassword ( viper . GetViper ( ) , walletGatePassphraseCfg )
gateKey , err := wallet . GetKeyFromPath ( viper . GetString ( gateWalletFlag ) , viper . GetString ( gateAddressFlag ) , gatePassword )
if err != nil {
2023-09-04 18:01:56 +00:00
return wrapPreparationError ( fmt . Errorf ( "failed to load s3 gate private key: %s" , err ) )
2023-06-23 13:12:18 +00:00
}
2024-08-30 12:05:32 +00:00
accessBox , accessKeyID , isCustom , err := getAccessBoxID ( )
if err != nil {
return wrapPreparationError ( err )
2023-06-23 13:12:18 +00:00
}
var gatesPublicKeys [ ] * keys . PublicKey
for _ , keyStr := range viper . GetStringSlice ( gatePublicKeyFlag ) {
gpk , err := keys . NewPublicKeyFromString ( keyStr )
if err != nil {
2023-09-04 18:01:56 +00:00
return wrapPreparationError ( fmt . Errorf ( "failed to load gate's public key: %s" , err ) )
2023-06-23 13:12:18 +00:00
}
gatesPublicKeys = append ( gatesPublicKeys , gpk )
}
poolCfg := PoolConfig {
2023-08-03 12:08:22 +00:00
Key : key ,
2023-06-23 13:12:18 +00:00
Address : viper . GetString ( peerFlag ) ,
DialTimeout : viper . GetDuration ( poolDialTimeoutFlag ) ,
HealthcheckTimeout : viper . GetDuration ( poolHealthcheckTimeoutFlag ) ,
StreamTimeout : viper . GetDuration ( poolStreamTimeoutFlag ) ,
RebalanceInterval : viper . GetDuration ( poolRebalanceIntervalFlag ) ,
}
frostFS , err := createFrostFS ( ctx , log , poolCfg )
if err != nil {
2023-09-04 18:01:56 +00:00
return wrapFrostFSInitError ( fmt . Errorf ( "failed to create FrostFS component: %s" , err ) )
2023-06-23 13:12:18 +00:00
}
2024-01-19 07:00:04 +00:00
customAttrs , err := parseObjectAttrs ( viper . GetString ( attributesFlag ) )
if err != nil {
return wrapPreparationError ( fmt . Errorf ( "failed to parse attributes: %s" , err ) )
}
2023-06-23 13:12:18 +00:00
updateSecretOptions := & authmate . UpdateSecretOptions {
2024-08-30 12:05:32 +00:00
ContainerID : accessBox ,
AccessKeyID : accessKeyID ,
IsCustom : isCustom ,
2024-01-19 07:00:04 +00:00
FrostFSKey : key ,
GatesPublicKeys : gatesPublicKeys ,
GatePrivateKey : gateKey ,
CustomAttributes : customAttrs ,
2023-06-23 13:12:18 +00:00
}
if err = authmate . New ( log , frostFS ) . UpdateSecret ( ctx , os . Stdout , updateSecretOptions ) ; err != nil {
2023-09-04 18:01:56 +00:00
return wrapBusinessLogicError ( fmt . Errorf ( "failed to update secret: %s" , err ) )
2023-06-23 13:12:18 +00:00
}
return nil
}