Skip to content

Commit 2f52f60

Browse files
committed
fix: unify the auth data handle to the decode method
Signed-off-by: chlins <[email protected]>
1 parent a548ab7 commit 2f52f60

File tree

3 files changed

+191
-8
lines changed

3 files changed

+191
-8
lines changed

src/pkg/p2p/preheat/instance/manager.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ package instance
1616

1717
import (
1818
"context"
19-
"encoding/json"
2019

2120
"github.com/goharbor/harbor/src/lib/q"
2221
dao "github.com/goharbor/harbor/src/pkg/p2p/preheat/dao/instance"
@@ -119,19 +118,26 @@ func (dm *manager) Get(ctx context.Context, id int64) (*provider.Instance, error
119118
if err != nil {
120119
return nil, err
121120
}
122-
// mapping auth data to auth info.
123-
if len(ins.AuthData) > 0 {
124-
if err := json.Unmarshal([]byte(ins.AuthData), &ins.AuthInfo); err != nil {
125-
return nil, err
126-
}
121+
122+
if err := ins.Decode(); err != nil {
123+
return nil, err
127124
}
128125

129126
return ins, nil
130127
}
131128

132129
// Get implements @Manager.GetByName
133130
func (dm *manager) GetByName(ctx context.Context, name string) (*provider.Instance, error) {
134-
return dm.dao.GetByName(ctx, name)
131+
ins, err := dm.dao.GetByName(ctx, name)
132+
if err != nil {
133+
return nil, err
134+
}
135+
136+
if err := ins.Decode(); err != nil {
137+
return nil, err
138+
}
139+
140+
return ins, nil
135141
}
136142

137143
// Count implements @Manager.Count
@@ -141,5 +147,16 @@ func (dm *manager) Count(ctx context.Context, query *q.Query) (int64, error) {
141147

142148
// List implements @Manager.List
143149
func (dm *manager) List(ctx context.Context, query *q.Query) ([]*provider.Instance, error) {
144-
return dm.dao.List(ctx, query)
150+
inss, err := dm.dao.List(ctx, query)
151+
if err != nil {
152+
return nil, err
153+
}
154+
155+
for i := range inss {
156+
if err := inss[i].Decode(); err != nil {
157+
return nil, err
158+
}
159+
}
160+
161+
return inss, nil
145162
}

src/pkg/p2p/preheat/models/provider/instance.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,33 @@ func (ins *Instance) ToJSON() (string, error) {
8282
return string(data), nil
8383
}
8484

85+
// Decode decodes the instance.
86+
func (ins *Instance) Decode() error {
87+
// decode the auth data.
88+
authInfo, err := decodeAuthData(ins.AuthData)
89+
if err != nil {
90+
return err
91+
}
92+
93+
if len(authInfo) > 0 {
94+
ins.AuthInfo = authInfo
95+
}
96+
97+
return nil
98+
}
99+
100+
// decodeAuthData decodes the auth data.
101+
func decodeAuthData(data string) (map[string]string, error) {
102+
authInfo := make(map[string]string)
103+
if len(data) > 0 {
104+
if err := json.Unmarshal([]byte(data), &authInfo); err != nil {
105+
return nil, errors.Wrap(err, "decode auth data error")
106+
}
107+
}
108+
109+
return authInfo, nil
110+
}
111+
85112
// TableName ...
86113
func (ins *Instance) TableName() string {
87114
return "p2p_preheat_instance"
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright Project Harbor Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package provider
16+
17+
import (
18+
"encoding/json"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
)
23+
24+
func TestInstance_FromJSON(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
json string
28+
wantErr bool
29+
}{
30+
{
31+
name: "Empty JSON",
32+
json: "",
33+
wantErr: true,
34+
},
35+
{
36+
name: "Invalid JSON",
37+
json: "{invalid}",
38+
wantErr: true,
39+
},
40+
{
41+
name: "Valid JSON",
42+
json: `{
43+
"id": 1,
44+
"name": "test-instance",
45+
"description": "test description",
46+
"vendor": "test-vendor",
47+
"endpoint": "http://test-endpoint",
48+
"auth_mode": "basic",
49+
"auth_info": {"username": "test", "password": "test123"},
50+
"enabled": true,
51+
"default": false,
52+
"insecure": false,
53+
"setup_timestamp": 1234567890
54+
}`,
55+
wantErr: false,
56+
},
57+
}
58+
59+
for _, tt := range tests {
60+
t.Run(tt.name, func(t *testing.T) {
61+
ins := &Instance{}
62+
err := ins.FromJSON(tt.json)
63+
if tt.wantErr {
64+
assert.Error(t, err)
65+
} else {
66+
assert.NoError(t, err)
67+
assert.Equal(t, int64(1), ins.ID)
68+
assert.Equal(t, "test-instance", ins.Name)
69+
}
70+
})
71+
}
72+
}
73+
74+
func TestInstance_ToJSON(t *testing.T) {
75+
ins := &Instance{
76+
ID: 1,
77+
Name: "test-instance",
78+
Description: "test description",
79+
Vendor: "test-vendor",
80+
Endpoint: "http://test-endpoint",
81+
AuthMode: "basic",
82+
AuthInfo: map[string]string{"username": "test", "password": "test123"},
83+
Enabled: true,
84+
Default: false,
85+
Insecure: false,
86+
}
87+
88+
jsonStr, err := ins.ToJSON()
89+
assert.NoError(t, err)
90+
91+
// Verify the JSON can be decoded back
92+
var decoded Instance
93+
err = json.Unmarshal([]byte(jsonStr), &decoded)
94+
assert.NoError(t, err)
95+
assert.Equal(t, ins.ID, decoded.ID)
96+
assert.Equal(t, ins.Name, decoded.Name)
97+
assert.Equal(t, ins.AuthInfo, decoded.AuthInfo)
98+
}
99+
100+
func TestInstance_Decode(t *testing.T) {
101+
tests := []struct {
102+
name string
103+
authData string
104+
wantErr bool
105+
}{
106+
{
107+
name: "Empty auth data",
108+
authData: "",
109+
wantErr: false,
110+
},
111+
{
112+
name: "Invalid auth data",
113+
authData: "{invalid}",
114+
wantErr: true,
115+
},
116+
{
117+
name: "Valid auth data",
118+
authData: `{"username": "test", "password": "test123"}`,
119+
wantErr: false,
120+
},
121+
}
122+
123+
for _, tt := range tests {
124+
t.Run(tt.name, func(t *testing.T) {
125+
ins := &Instance{
126+
AuthData: tt.authData,
127+
}
128+
err := ins.Decode()
129+
if tt.wantErr {
130+
assert.Error(t, err)
131+
} else {
132+
assert.NoError(t, err)
133+
if tt.authData != "" {
134+
assert.NotEmpty(t, ins.AuthInfo)
135+
}
136+
}
137+
})
138+
}
139+
}

0 commit comments

Comments
 (0)