@@ -218,21 +218,22 @@ func (reader *AmaasClientBufferReader) Hash(algorithm string) (string, error) {
218218///////////////////////////////////////
219219
220220type AmaasClient struct {
221- conn * grpc.ClientConn
222- isC1Token bool
223- authKey string
224- addr string
225- useTLS bool
226- caCert string
227- verifyCert bool
228- timeoutSecs int
229- appName string
230- archHandler AmaasClientArchiveHandler
231- pml bool
232- feedback bool
233- verbose bool
234- activeContent bool
235- digest bool
221+ conn * grpc.ClientConn
222+ isC1Token bool
223+ authKey string
224+ addr string
225+ useTLS bool
226+ caCert string
227+ verifyCert bool
228+ timeoutSecs int
229+ appName string
230+ archHandler AmaasClientArchiveHandler
231+ pml bool
232+ feedback bool
233+ verbose bool
234+ activeContent bool
235+ digest bool
236+ cloudAccountID string
236237}
237238
238239func getHashValue (dataReader AmaasClientReader ) (string , string , error ) {
@@ -446,8 +447,7 @@ func runUploadLoop(stream pb.Scan_RunClient, dataReader AmaasClientReader, bulk
446447 return
447448}
448449
449- func (ac * AmaasClient ) bufferScanRun (buffer []byte , identifier string , tags []string ) (string , error ) {
450-
450+ func (ac * AmaasClient ) bufferScanRun (ctx context.Context , buffer []byte , identifier string , tags []string ) (string , error ) {
451451 if ac .conn == nil {
452452 return "" , makeInternalError (MSG ("MSG_ID_ERR_CLIENT_NOT_READY" ))
453453 }
@@ -458,18 +458,20 @@ func (ac *AmaasClient) bufferScanRun(buffer []byte, identifier string, tags []st
458458 }
459459 defer bufferReader .Close ()
460460
461- ctx , cancel := context .WithTimeout (context . Background () , time .Second * time .Duration (ac .timeoutSecs ))
461+ ctx , cancel := context .WithTimeout (ctx , time .Second * time .Duration (ac .timeoutSecs ))
462462
463463 ctx = ac .buildAuthContext (ctx )
464464
465465 ctx = ac .buildAppNameContext (ctx )
466466
467- return scanRun (ctx , cancel , pb .NewScanClient (ac .conn ), bufferReader , tags , ac .pml , true , ac .feedback ,
467+ tags = ac .appendCloudAccountIDToTags (tags )
468+
469+ return scanRun (ctx , cancel , pb .NewScanClient (ac .conn ), bufferReader ,
470+ tags , ac .pml , true , ac .feedback ,
468471 ac .verbose , ac .activeContent , ac .digest )
469472}
470473
471- func (ac * AmaasClient ) fileScanRun (fileName string , tags []string ) (string , error ) {
472-
474+ func (ac * AmaasClient ) fileScanRun (ctx context.Context , fileName string , tags []string ) (string , error ) {
473475 if ac .conn == nil {
474476 return "" , makeInternalError (MSG ("MSG_ID_ERR_CLIENT_NOT_READY" ))
475477 }
@@ -478,40 +480,46 @@ func (ac *AmaasClient) fileScanRun(fileName string, tags []string) (string, erro
478480 return ac .archHandler .fileScanRun (fileName )
479481 }
480482
481- return ac .fileScanRunNormalFile (fileName , tags )
483+ return ac .fileScanRunNormalFile (ctx , fileName , tags )
482484}
483485
484- func (ac * AmaasClient ) fileScanRunNormalFile (fileName string , tags []string ) (string , error ) {
486+ func (ac * AmaasClient ) fileScanRunNormalFile (ctx context. Context , fileName string , tags []string ) (string , error ) {
485487
486488 fileReader , err := InitFileReader (fileName )
487489 if err != nil {
488490 return "" , makeInternalError (fmt .Sprintf (MSG ("MSG_ID_ERR_CLIENT_ERROR" ), err .Error ()))
489491 }
490492 defer fileReader .Close ()
491493
492- ctx , cancel := context .WithTimeout (context . Background () , time .Second * time .Duration (ac .timeoutSecs ))
494+ ctx , cancel := context .WithTimeout (ctx , time .Second * time .Duration (ac .timeoutSecs ))
493495
494496 ctx = ac .buildAuthContext (ctx )
495497
496498 ctx = ac .buildAppNameContext (ctx )
497499
498- return scanRun (ctx , cancel , pb .NewScanClient (ac .conn ), fileReader , tags , ac .pml , true , ac .feedback ,
500+ tags = ac .appendCloudAccountIDToTags (tags )
501+
502+ return scanRun (ctx , cancel , pb .NewScanClient (ac .conn ), fileReader ,
503+ tags , ac .pml , true , ac .feedback ,
499504 ac .verbose , ac .activeContent , ac .digest )
500505}
501506
502- func (ac * AmaasClient ) readerScanRun (reader AmaasClientReader , tags []string ) (string , error ) {
507+ func (ac * AmaasClient ) readerScanRun (ctx context. Context , reader AmaasClientReader , tags []string ) (string , error ) {
503508
504509 if ac .conn == nil {
505510 return "" , makeInternalError (MSG ("MSG_ID_ERR_CLIENT_NOT_READY" ))
506511 }
507512
508- ctx , cancel := context .WithTimeout (context . Background () , time .Second * time .Duration (ac .timeoutSecs ))
513+ ctx , cancel := context .WithTimeout (ctx , time .Second * time .Duration (ac .timeoutSecs ))
509514
510515 ctx = ac .buildAuthContext (ctx )
511516
512517 ctx = ac .buildAppNameContext (ctx )
513518
514- return scanRun (ctx , cancel , pb .NewScanClient (ac .conn ), reader , tags , ac .pml , true , ac .feedback ,
519+ tags = ac .appendCloudAccountIDToTags (tags )
520+
521+ return scanRun (ctx , cancel , pb .NewScanClient (ac .conn ), reader ,
522+ tags , ac .pml , true , ac .feedback ,
515523 ac .verbose , ac .activeContent , ac .digest )
516524}
517525
@@ -1127,6 +1135,22 @@ func (ac *AmaasClient) SetDigestDisable() {
11271135 ac .digest = false
11281136}
11291137
1138+ func (ac * AmaasClient ) SetCloudAccountID (cloudAccountID string ) error {
1139+ if cloudAccountID == "" {
1140+ ac .cloudAccountID = cloudAccountID
1141+ return nil
1142+ }
1143+
1144+ // Calculate the total tag length with "cloudAccountId=" prefix
1145+ cloudAccountTag := fmt .Sprintf ("cloudAccountId=%s" , cloudAccountID )
1146+ if len (cloudAccountTag ) > maxTagSize {
1147+ return fmt .Errorf ("cloudAccountID tag 'cloudAccountId=%s' exceeds maximum tag size of %d characters" , cloudAccountID , maxTagSize )
1148+ }
1149+
1150+ ac .cloudAccountID = cloudAccountID
1151+ return nil
1152+ }
1153+
11301154func validateTags (tags []string ) error {
11311155 if len (tags ) == 0 {
11321156 return errors .New ("tags cannot be empty" )
@@ -1146,3 +1170,23 @@ func validateTags(tags []string) error {
11461170 }
11471171 return nil
11481172}
1173+
1174+ func (ac * AmaasClient ) appendCloudAccountIDToTags (tags []string ) []string {
1175+ if ac .cloudAccountID == "" {
1176+ return tags
1177+ }
1178+
1179+ cloudAccountTag := fmt .Sprintf ("cloudAccountId=%s" , ac .cloudAccountID )
1180+
1181+ // Check if the cloudAccountTag exceeds maxTagSize (63 characters)
1182+ if len (cloudAccountTag ) > maxTagSize {
1183+ logMsg (LogLevelWarning , "cloudAccountId tag exceeds maximum tag size (%d), skipping" , maxTagSize )
1184+ return tags
1185+ }
1186+
1187+ if tags == nil {
1188+ return []string {cloudAccountTag }
1189+ }
1190+
1191+ return append (tags , cloudAccountTag )
1192+ }
0 commit comments