Skip to content

Commit

Permalink
code review changes
Browse files Browse the repository at this point in the history
Signed-off-by: Min Min <[email protected]>
  • Loading branch information
Min Min committed Dec 31, 2021
1 parent 98cefee commit e59744c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 52 deletions.
78 changes: 36 additions & 42 deletions pkg/microservice/aslan/core/common/service/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ type awsKeyWithExpiration struct {
}

func (k *awsKeyWithExpiration) IsExpired() bool {
if time.Now().Unix() > k.Expiration {
return true
}
return false
return time.Now().Unix() > k.Expiration
}

func FindRegistryById(registryId string, getRealCredential bool, log *zap.SugaredLogger) (*models.RegistryNamespace, error) {
Expand All @@ -75,21 +72,20 @@ func findRegisty(regOps *mongodb.FindRegOps, getRealCredential bool, log *zap.Su
}

if getRealCredential {
switch resp.RegProvider {
case config.RegistryTypeSWR:
resp.AccessKey = fmt.Sprintf("%s@%s", resp.Region, resp.AccessKey)
resp.SecretKey = util.ComputeHmacSha256(resp.AccessKey, resp.SecretKey)
case config.RegistryTypeAWS:
realAK, realSK, err := getAWSRegistryCredential(resp.ID.Hex(), resp.AccessKey, resp.SecretKey, resp.Region)
if err != nil {
log.Errorf("Failed to get keypair from aws, the error is: %s", err)
return nil, err
}
resp.AccessKey = realAK
resp.SecretKey = realSK
default:
break
return resp, nil
}
switch resp.RegProvider {
case config.RegistryTypeSWR:
resp.AccessKey = fmt.Sprintf("%s@%s", resp.Region, resp.AccessKey)
resp.SecretKey = util.ComputeHmacSha256(resp.AccessKey, resp.SecretKey)
case config.RegistryTypeAWS:
realAK, realSK, err := getAWSRegistryCredential(resp.ID.Hex(), resp.AccessKey, resp.SecretKey, resp.Region)
if err != nil {
log.Errorf("Failed to get keypair from aws, the error is: %s", err)
return nil, err
}
resp.AccessKey = realAK
resp.SecretKey = realSK
}

return resp, nil
Expand All @@ -106,26 +102,25 @@ func ListRegistryNamespaces(getRealCredential bool, log *zap.SugaredLogger) ([]*
return resp, fmt.Errorf("RegistryNamespace.List error: %s", err)
}

if getRealCredential {
for _, reg := range resp {
switch reg.RegProvider {
case config.RegistryTypeSWR:
reg.AccessKey = fmt.Sprintf("%s@%s", reg.Region, reg.AccessKey)
reg.SecretKey = util.ComputeHmacSha256(reg.AccessKey, reg.SecretKey)
case config.RegistryTypeAWS:
realAK, realSK, err := getAWSRegistryCredential(reg.ID.Hex(), reg.AccessKey, reg.SecretKey, reg.Region)
if err != nil {
log.Errorf("Failed to get keypair from aws, the error is: %s", err)
return nil, err
}
reg.AccessKey = realAK
reg.SecretKey = realSK
default:
break
if !getRealCredential {
return resp, nil
}

for _, reg := range resp {
switch reg.RegProvider {
case config.RegistryTypeSWR:
reg.AccessKey = fmt.Sprintf("%s@%s", reg.Region, reg.AccessKey)
reg.SecretKey = util.ComputeHmacSha256(reg.AccessKey, reg.SecretKey)
case config.RegistryTypeAWS:
realAK, realSK, err := getAWSRegistryCredential(reg.ID.Hex(), reg.AccessKey, reg.SecretKey, reg.Region)
if err != nil {
log.Errorf("Failed to get keypair from aws, the error is: %s", err)
return nil, err
}
reg.AccessKey = realAK
reg.SecretKey = realSK
}
}

return resp, nil
}

Expand Down Expand Up @@ -161,21 +156,20 @@ func EnsureDefaultRegistrySecret(namespace string, registryId string, kubeClient
return nil
}

func getAWSRegistryCredential(ID, AK, SK, Region string) (string, string, error) {
func getAWSRegistryCredential(id, ak, sk, region string) (realAK string, realSK string, err error) {
// first we try to get ak/sk from our memory cache
obj, ok := awsKeyMap.Load(ID)
obj, ok := awsKeyMap.Load(id)
if ok {
keypair, ok := obj.(awsKeyWithExpiration)
if ok {
if !keypair.IsExpired() {
fmt.Printf("Getting aws ak/sk from memory cache: ak[%s], sk[%s]", keypair.AccessKey, keypair.SecretKey)
return keypair.AccessKey, keypair.SecretKey, nil
}
}
}
creds := credentials.NewStaticCredentials(AK, SK, "")
creds := credentials.NewStaticCredentials(ak, sk, "")
config := &aws.Config{
Region: aws.String(Region),
Region: aws.String(region),
Credentials: creds,
}
sess, err := session.NewSession(config)
Expand All @@ -197,10 +191,10 @@ func getAWSRegistryCredential(ID, AK, SK, Region string) (string, string, error)
}
keypair := strings.Split(string(rawDecodedText), ":")
if len(keypair) != 2 {
return "", "", errors.New("decode keypair from aws response error")
return "", "", errors.New("format of keypair is invalid")
}
// cache the aws ak/sk
awsKeyMap.Store(ID, awsKeyWithExpiration{
awsKeyMap.Store(id, awsKeyWithExpiration{
AccessKey: keypair[0],
SecretKey: keypair[1],
Expiration: time.Now().Add(expirationTime).Unix(),
Expand Down
22 changes: 12 additions & 10 deletions pkg/microservice/aslan/core/common/service/registry/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ type Service interface {
func NewV2Service(provider string) Service {
switch provider {
case config.RegistryTypeSWR:
return &SwrService{}
return &swrService{}
case config.RegistryTypeAWS:
return &EcrService{}
return &ecrService{}
default:
return &v2RegistryService{}
}
Expand Down Expand Up @@ -362,10 +362,10 @@ func (s *v2RegistryService) ListRepoImages(option ListRepoImagesOption, log *zap
return resp, nil
}

type SwrService struct {
type swrService struct {
}

func (s *SwrService) createClient(ep Endpoint) (cli *swr.SwrClient) {
func (s *swrService) createClient(ep Endpoint) (cli *swr.SwrClient) {
endpoint := fmt.Sprintf("https://swr-api.%s.myhuaweicloud.com", ep.Region)
auth := basic.NewCredentialsBuilder().
WithAk(ep.Ak).
Expand All @@ -380,7 +380,7 @@ func (s *SwrService) createClient(ep Endpoint) (cli *swr.SwrClient) {
return client
}

func (s *SwrService) ListRepoImages(option ListRepoImagesOption, log *zap.SugaredLogger) (resp *ReposResp, err error) {
func (s *swrService) ListRepoImages(option ListRepoImagesOption, log *zap.SugaredLogger) (resp *ReposResp, err error) {
swrCli := s.createClient(option.Endpoint)

var wg wait.Group
Expand Down Expand Up @@ -435,7 +435,7 @@ func (s *SwrService) ListRepoImages(option ListRepoImagesOption, log *zap.Sugare

}

func (s *SwrService) GetImageInfo(option GetRepoImageDetailOption, log *zap.SugaredLogger) (di *commonmodels.DeliveryImage, err error) {
func (s *swrService) GetImageInfo(option GetRepoImageDetailOption, log *zap.SugaredLogger) (di *commonmodels.DeliveryImage, err error) {
swrCli := s.createClient(option.Endpoint)

request := &model.ListRepositoryTagsRequest{Tag: &option.Tag, Namespace: option.Namespace, Repository: option.Image}
Expand All @@ -458,10 +458,10 @@ func (s *SwrService) GetImageInfo(option GetRepoImageDetailOption, log *zap.Suga
return &commonmodels.DeliveryImage{}, nil
}

type EcrService struct {
type ecrService struct {
}

func (s *EcrService) getECRService(ep Endpoint, log *zap.SugaredLogger) (*ecr.ECR, error) {
func (s *ecrService) getECRService(ep Endpoint, log *zap.SugaredLogger) (*ecr.ECR, error) {
creds := credentials.NewStaticCredentials(ep.Ak, ep.Sk, "")
config := &aws.Config{
Region: aws.String(ep.Region),
Expand All @@ -475,7 +475,7 @@ func (s *EcrService) getECRService(ep Endpoint, log *zap.SugaredLogger) (*ecr.EC
return ecr.New(sess), nil
}

func (s *EcrService) ListRepoImages(option ListRepoImagesOption, log *zap.SugaredLogger) (resp *ReposResp, err error) {
func (s *ecrService) ListRepoImages(option ListRepoImagesOption, log *zap.SugaredLogger) (resp *ReposResp, err error) {
svc, err := s.getECRService(option.Endpoint, log)
if err != nil {
return nil, err
Expand Down Expand Up @@ -527,7 +527,7 @@ func (s *EcrService) ListRepoImages(option ListRepoImagesOption, log *zap.Sugare
return resp, nil
}

func (s *EcrService) GetImageInfo(option GetRepoImageDetailOption, log *zap.SugaredLogger) (di *commonmodels.DeliveryImage, err error) {
func (s *ecrService) GetImageInfo(option GetRepoImageDetailOption, log *zap.SugaredLogger) (di *commonmodels.DeliveryImage, err error) {
svc, err := s.getECRService(option.Endpoint, log)
if err != nil {
return nil, err
Expand All @@ -545,6 +545,8 @@ func (s *EcrService) GetImageInfo(option GetRepoImageDetailOption, log *zap.Suga
err = errors.Wrapf(err, "failed to get image info of %s:%s", option.Image, option.Tag)
return
}
// since only one image tag is passed, only one image detail will be in this detail list
// so only the first one will be used
for _, imageDetail := range result.ImageDetails {
return &commonmodels.DeliveryImage{
RepoName: option.Image,
Expand Down

0 comments on commit e59744c

Please sign in to comment.