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
if registry.Credential != nil && len(registry.Credential.AccessSecret) != 0 {
registry.Credential.AccessSecret = "*****" registry.Credential.AccessSecret = "*****"
}
t.Data["json"] = registry t.Data["json"] = registry
t.ServeJSON() t.ServeJSON()
@ -76,8 +78,10 @@ func (t *RegistryAPI) List() {
// Hide passwords // Hide passwords
for _, registry := range registries { for _, registry := range registries {
if registry.Credential != nil && len(registry.Credential.AccessSecret) != 0 {
registry.Credential.AccessSecret = "*****" registry.Credential.AccessSecret = "*****"
} }
}
t.Data["json"] = registries t.Data["json"] = registries
t.ServeJSON() t.ServeJSON()

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
var err error
if len(registry.AccessSecret) != 0 {
decrypted, err = decrypt(registry.AccessSecret)
if err != nil { if err != nil {
return nil, err 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),
Credential: &model.Credential{},
URL: registry.URL, URL: registry.URL,
Credential: &model.Credential{
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
if registry.Credential != nil {
encrypted, err = encrypt(registry.Credential.AccessSecret)
if err != nil {
return nil, err
}
}
return &models.Registry{
ID: registry.ID, ID: registry.ID,
URL: registry.URL, URL: registry.URL,
Name: registry.Name, Name: registry.Name,
CredentialType: string(registry.Credential.Type),
AccessKey: registry.Credential.AccessKey,
AccessSecret: encrypted,
Type: string(registry.Type), Type: string(registry.Type),
Insecure: registry.Insecure, Insecure: registry.Insecure,
Description: registry.Description, Description: registry.Description,
Health: registry.Status, Health: registry.Status,
CreationTime: registry.CreationTime, CreationTime: registry.CreationTime,
UpdateTime: registry.UpdateTime, UpdateTime: registry.UpdateTime,
}, nil }
if registry.Credential != nil && len(registry.Credential.AccessKey) != 0 {
var encrypted string
var err error
if len(registry.Credential.AccessSecret) != 0 {
encrypted, err = encrypt(registry.Credential.AccessSecret)
if err != nil {
return nil, err
}
}
m.CredentialType = string(registry.Credential.Type)
m.AccessKey = registry.Credential.AccessKey
m.AccessSecret = encrypted
}
return m, nil
} }