Skip to content

Commit 52f0af0

Browse files
committed
[#372] Check parameters before creating container
Signed-off-by: Denis Kirillov <[email protected]>
1 parent 02f4524 commit 52f0af0

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

authmate/authmate.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -230,16 +230,7 @@ func (a *Agent) IssueSecret(ctx context.Context, w io.Writer, options *IssueSecr
230230
lifetime.Exp = lifetime.Iat + epochLifetime
231231
}
232232

233-
idOwner := owner.NewIDFromPublicKey(&options.NeoFSKey.PrivateKey.PublicKey)
234-
235-
a.log.Info("check container or create", zap.Stringer("cid", options.Container.ID),
236-
zap.String("friendly_name", options.Container.FriendlyName),
237-
zap.String("placement_policy", options.Container.PlacementPolicy))
238-
if id, err = a.checkContainer(ctx, options.Container, idOwner); err != nil {
239-
return err
240-
}
241-
242-
gatesData, err := createTokens(options, lifetime, id)
233+
gatesData, err := createTokens(options, lifetime)
243234
if err != nil {
244235
return err
245236
}
@@ -251,6 +242,15 @@ func (a *Agent) IssueSecret(ctx context.Context, w io.Writer, options *IssueSecr
251242

252243
box.ContainerPolicy = policies
253244

245+
idOwner := owner.NewIDFromPublicKey(&options.NeoFSKey.PrivateKey.PublicKey)
246+
247+
a.log.Info("check container or create", zap.Stringer("cid", options.Container.ID),
248+
zap.String("friendly_name", options.Container.FriendlyName),
249+
zap.String("placement_policy", options.Container.PlacementPolicy))
250+
if id, err = a.checkContainer(ctx, options.Container, idOwner); err != nil {
251+
return err
252+
}
253+
254254
a.log.Info("store bearer token into NeoFS",
255255
zap.Stringer("owner_tkn", idOwner))
256256

@@ -318,7 +318,7 @@ func (a *Agent) ObtainSecret(ctx context.Context, w io.Writer, options *ObtainSe
318318
return enc.Encode(or)
319319
}
320320

321-
func buildEACLTable(cid *cid.ID, eaclTable []byte) (*eacl.Table, error) {
321+
func buildEACLTable(eaclTable []byte) (*eacl.Table, error) {
322322
table := eacl.NewTable()
323323
if len(eaclTable) != 0 {
324324
return table, table.UnmarshalJSON(eaclTable)
@@ -332,7 +332,6 @@ func buildEACLTable(cid *cid.ID, eaclTable []byte) (*eacl.Table, error) {
332332
// matcher := eacl.MatchStringEqual
333333
// record.AddFilter(from eacl.FilterHeaderType, matcher eacl.Match, name string, value string)
334334
eacl.AddFormedTarget(record, eacl.RoleOthers)
335-
table.SetCID(cid)
336335
table.AddRecord(record)
337336

338337
return table, nil
@@ -437,10 +436,10 @@ func buildSessionTokens(key *keys.PrivateKey, oid *owner.ID, lifetime lifetimeOp
437436
return sessionTokens, nil
438437
}
439438

440-
func createTokens(options *IssueSecretOptions, lifetime lifetimeOptions, cid *cid.ID) ([]*accessbox.GateData, error) {
439+
func createTokens(options *IssueSecretOptions, lifetime lifetimeOptions) ([]*accessbox.GateData, error) {
441440
gates := make([]*accessbox.GateData, len(options.GatesPublicKeys))
442441

443-
table, err := buildEACLTable(cid, options.EACLRules)
442+
table, err := buildEACLTable(options.EACLRules)
444443
if err != nil {
445444
return nil, fmt.Errorf("failed to build eacl table: %w", err)
446445
}

cmd/authmate/main.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,16 @@ It will be ceil rounded to the nearest amount of epoch.`,
271271
return cli.Exit(fmt.Sprintf("couldn't parse container policy: %s", err.Error()), 6)
272272
}
273273

274+
bearerRules, err := getJSONRules(eaclRulesFlag)
275+
if err != nil {
276+
return cli.Exit(fmt.Sprintf("couldn't parse 'bearer-rules' flag: %s", err.Error()), 7)
277+
}
278+
279+
sessionRules, err := getSessionRules(sessionTokenFlag)
280+
if err != nil {
281+
return cli.Exit(fmt.Sprintf("couldn't parse 'session-token' flag: %s", err.Error()), 8)
282+
}
283+
274284
issueSecretOptions := &authmate.IssueSecretOptions{
275285
Container: authmate.ContainerOptions{
276286
ID: containerID,
@@ -279,8 +289,8 @@ It will be ceil rounded to the nearest amount of epoch.`,
279289
},
280290
NeoFSKey: key,
281291
GatesPublicKeys: gatesPublicKeys,
282-
EACLRules: getJSONRules(eaclRulesFlag),
283-
SessionTokenRules: getSessionRules(sessionTokenFlag),
292+
EACLRules: bearerRules,
293+
SessionTokenRules: sessionRules,
284294
ContainerPolicies: policies,
285295
Lifetime: lifetimeFlag,
286296
AwsCliCredentialsFile: awcCliCredFile,
@@ -315,17 +325,27 @@ func parsePolicies(val string) (authmate.ContainerPolicies, error) {
315325
return policies, nil
316326
}
317327

318-
func getJSONRules(val string) []byte {
328+
func getJSONRules(val string) ([]byte, error) {
329+
if val == "" {
330+
return nil, nil
331+
}
332+
data := []byte(val)
333+
if json.Valid(data) {
334+
return data, nil
335+
}
336+
319337
if data, err := os.ReadFile(val); err == nil {
320-
return data
338+
if json.Valid(data) {
339+
return data, nil
340+
}
321341
}
322342

323-
return []byte(val)
343+
return nil, fmt.Errorf("coudln't read json file or its content is invalid")
324344
}
325345

326-
func getSessionRules(r string) []byte {
346+
func getSessionRules(r string) ([]byte, error) {
327347
if r == "none" {
328-
return nil
348+
return nil, nil
329349
}
330350
return getJSONRules(r)
331351
}

0 commit comments

Comments
 (0)