Merge pull request #7291 from cd1989/registry-with-empty-credential

Handle registry with empty credential
This commit is contained in:
Wenkai Yin 2019-04-03 21:35:32 +08:00 committed by GitHub
commit 48f02d0605
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 36 deletions

View File

@ -55,7 +55,9 @@ func (t *RegistryAPI) Get() {
} }
// Hide access secret // Hide access secret
registry.Credential.AccessSecret = "*****" if registry.Credential != nil && len(registry.Credential.AccessSecret) != 0 {
registry.Credential.AccessSecret = "*****"
}
t.Data["json"] = registry t.Data["json"] = registry
t.ServeJSON() t.ServeJSON()
@ -76,7 +78,9 @@ func (t *RegistryAPI) List() {
// Hide passwords // Hide passwords
for _, registry := range registries { for _, registry := range registries {
registry.Credential.AccessSecret = "*****" if registry.Credential != nil && len(registry.Credential.AccessSecret) != 0 {
registry.Credential.AccessSecret = "*****"
}
} }
t.Data["json"] = registries t.Data["json"] = registries

View File

@ -264,55 +264,68 @@ func encrypt(secret string) (string, error) {
// fromDaoModel converts DAO layer registry model to replication model. // fromDaoModel converts DAO layer registry model to replication model.
// Also, if access secret is provided, decrypt it. // Also, if access secret is provided, decrypt it.
func fromDaoModel(registry *models.Registry) (*model.Registry, error) { func fromDaoModel(registry *models.Registry) (*model.Registry, error) {
decrypted, err := decrypt(registry.AccessSecret) var decrypted string
if err != nil { var err error
return nil, err if len(registry.AccessSecret) != 0 {
decrypted, err = decrypt(registry.AccessSecret)
if err != nil {
return nil, err
}
} }
r := &model.Registry{ r := &model.Registry{
ID: registry.ID, ID: registry.ID,
Name: registry.Name, Name: registry.Name,
Description: registry.Description, Description: registry.Description,
Type: model.RegistryType(registry.Type), Type: model.RegistryType(registry.Type),
URL: registry.URL, Credential: &model.Credential{},
Credential: &model.Credential{ URL: registry.URL,
Type: model.CredentialType(registry.CredentialType),
AccessKey: registry.AccessKey,
AccessSecret: decrypted,
},
Insecure: registry.Insecure, Insecure: registry.Insecure,
Status: registry.Health, Status: registry.Health,
CreationTime: registry.CreationTime, CreationTime: registry.CreationTime,
UpdateTime: registry.UpdateTime, UpdateTime: registry.UpdateTime,
} }
if len(registry.CredentialType) != 0 && len(registry.AccessKey) != 0 {
r.Credential = &model.Credential{
Type: model.CredentialType(registry.CredentialType),
AccessKey: registry.AccessKey,
AccessSecret: decrypted,
}
}
return r, nil return r, nil
} }
// toDaoModel converts registry model from replication to DAO layer model. // toDaoModel converts registry model from replication to DAO layer model.
// Also, if access secret is provided, encrypt it. // Also, if access secret is provided, encrypt it.
func toDaoModel(registry *model.Registry) (*models.Registry, error) { func toDaoModel(registry *model.Registry) (*models.Registry, error) {
var encrypted string m := &models.Registry{
var err error ID: registry.ID,
if registry.Credential != nil { URL: registry.URL,
encrypted, err = encrypt(registry.Credential.AccessSecret) Name: registry.Name,
if err != nil { Type: string(registry.Type),
return nil, err Insecure: registry.Insecure,
} Description: registry.Description,
Health: registry.Status,
CreationTime: registry.CreationTime,
UpdateTime: registry.UpdateTime,
} }
return &models.Registry{ if registry.Credential != nil && len(registry.Credential.AccessKey) != 0 {
ID: registry.ID, var encrypted string
URL: registry.URL, var err error
Name: registry.Name, if len(registry.Credential.AccessSecret) != 0 {
CredentialType: string(registry.Credential.Type), encrypted, err = encrypt(registry.Credential.AccessSecret)
AccessKey: registry.Credential.AccessKey, if err != nil {
AccessSecret: encrypted, return nil, err
Type: string(registry.Type), }
Insecure: registry.Insecure, }
Description: registry.Description,
Health: registry.Status, m.CredentialType = string(registry.Credential.Type)
CreationTime: registry.CreationTime, m.AccessKey = registry.Credential.AccessKey
UpdateTime: registry.UpdateTime, m.AccessSecret = encrypted
}, nil }
return m, nil
} }