mirror of
https://github.com/BishopFox/cloudfox.git
synced 2026-11-11 09:41:24 -10:00
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package sdk
|
|
|
|
import (
|
|
"context"
|
|
"encoding/gob"
|
|
"fmt"
|
|
|
|
"github.com/BishopFox/cloudfox/internal"
|
|
"github.com/aws/aws-sdk-go-v2/service/codecommit"
|
|
codeCommitTypes "github.com/aws/aws-sdk-go-v2/service/codecommit/types"
|
|
"github.com/patrickmn/go-cache"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type AWSCodeCommitClientInterface interface {
|
|
ListRepositories(context.Context, *codecommit.ListRepositoriesInput, ...func(*codecommit.Options)) (*codecommit.ListRepositoriesOutput, error)
|
|
}
|
|
|
|
func init() {
|
|
gob.Register([]codeCommitTypes.RepositoryNameIdPair{})
|
|
}
|
|
|
|
func CachedCodeCommitListRepositories(client AWSCodeCommitClientInterface, accountID string, region string) ([]codeCommitTypes.RepositoryNameIdPair, error) {
|
|
var PaginationControl *string
|
|
var repositories []codeCommitTypes.RepositoryNameIdPair
|
|
cacheKey := fmt.Sprintf("%s-codecommit-ListRepositories-%s", accountID, region)
|
|
cached, found := internal.Cache.Get(cacheKey)
|
|
if found {
|
|
sharedLogger.WithFields(logrus.Fields{
|
|
"api": "codecommit:ListRepositories",
|
|
"account": accountID,
|
|
"region": region,
|
|
"cache": "hit",
|
|
}).Info("AWS API call")
|
|
return cached.([]codeCommitTypes.RepositoryNameIdPair), nil
|
|
}
|
|
sharedLogger.WithFields(logrus.Fields{
|
|
"api": "codecommit:ListRepositories",
|
|
"account": accountID,
|
|
"region": region,
|
|
"cache": "miss",
|
|
}).Info("AWS API call")
|
|
for {
|
|
ListRepositories, err := client.ListRepositories(
|
|
context.TODO(),
|
|
&codecommit.ListRepositoriesInput{
|
|
NextToken: PaginationControl,
|
|
},
|
|
func(o *codecommit.Options) {
|
|
o.Region = region
|
|
},
|
|
)
|
|
|
|
if err != nil {
|
|
return repositories, err
|
|
}
|
|
|
|
repositories = append(repositories, ListRepositories.Repositories...)
|
|
|
|
//pagination
|
|
if ListRepositories.NextToken == nil {
|
|
break
|
|
}
|
|
PaginationControl = ListRepositories.NextToken
|
|
}
|
|
|
|
internal.Cache.Set(cacheKey, repositories, cache.DefaultExpiration)
|
|
return repositories, nil
|
|
}
|