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
registry.Credential.AccessSecret = "*****"
if registry.Credential != nil && len(registry.Credential.AccessSecret) != 0 {
registry.Credential.AccessSecret = "*****"
}
t.Data["json"] = registry
t.ServeJSON()
@ -76,7 +78,9 @@ func (t *RegistryAPI) List() {
// Hide passwords
for _, registry := range registries {
registry.Credential.AccessSecret = "*****"
if registry.Credential != nil && len(registry.Credential.AccessSecret) != 0 {
registry.Credential.AccessSecret = "*****"
}
}
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.
// Also, if access secret is provided, decrypt it.
func fromDaoModel(registry *models.Registry) (*model.Registry, error) {
decrypted, err := decrypt(registry.AccessSecret)
if err != nil {
return nil, err
var decrypted string
var err error
if len(registry.AccessSecret) != 0 {
decrypted, err = decrypt(registry.AccessSecret)
if err != nil {
return nil, err
}
}
r := &model.Registry{
ID: registry.ID,
Name: registry.Name,
Description: registry.Description,
Type: model.RegistryType(registry.Type),
URL: registry.URL,
Credential: &model.Credential{
Type: model.CredentialType(registry.CredentialType),
AccessKey: registry.AccessKey,
AccessSecret: decrypted,
},
ID: registry.ID,
Name: registry.Name,
Description: registry.Description,
Type: model.RegistryType(registry.Type),
Credential: &model.Credential{},
URL: registry.URL,
Insecure: registry.Insecure,
Status: registry.Health,
CreationTime: registry.CreationTime,
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
}
// toDaoModel converts registry model from replication to DAO layer model.
// Also, if access secret is provided, encrypt it.
func toDaoModel(registry *model.Registry) (*models.Registry, error) {
var encrypted string
var err error
if registry.Credential != nil {
encrypted, err = encrypt(registry.Credential.AccessSecret)
if err != nil {
return nil, err
}
m := &models.Registry{
ID: registry.ID,
URL: registry.URL,
Name: registry.Name,
Type: string(registry.Type),
Insecure: registry.Insecure,
Description: registry.Description,
Health: registry.Status,
CreationTime: registry.CreationTime,
UpdateTime: registry.UpdateTime,
}
return &models.Registry{
ID: registry.ID,
URL: registry.URL,
Name: registry.Name,
CredentialType: string(registry.Credential.Type),
AccessKey: registry.Credential.AccessKey,
AccessSecret: encrypted,
Type: string(registry.Type),
Insecure: registry.Insecure,
Description: registry.Description,
Health: registry.Status,
CreationTime: registry.CreationTime,
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
}