-
Notifications
You must be signed in to change notification settings - Fork 572
/
harbor.go
115 lines (95 loc) · 2.85 KB
/
harbor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package harbor
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/deepfence/ThreatMapper/deepfence_server/model"
"github.com/deepfence/ThreatMapper/deepfence_utils/encryption"
"github.com/deepfence/ThreatMapper/deepfence_utils/log"
"github.com/deepfence/ThreatMapper/deepfence_utils/telemetry"
"github.com/go-playground/validator/v10"
)
func New(data []byte) (*RegistryHarbor, error) {
var r RegistryHarbor
err := json.Unmarshal(data, &r)
if err != nil {
return nil, err
}
return &r, nil
}
func (d *RegistryHarbor) ValidateFields(v *validator.Validate) error {
return v.Struct(d)
}
func (d *RegistryHarbor) IsValidCredential() bool {
if d.NonSecret.HarborRegistryURL == "" {
return false
}
listRepos := "%s/api/v2.0/projects/%s/repositories"
url := fmt.Sprintf(listRepos, d.NonSecret.HarborRegistryURL, d.NonSecret.HarborProjectName)
req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
log.Error().Msg(err.Error())
return false
}
req.Header.Set("Content-Type", "application/json")
req.SetBasicAuth(d.NonSecret.HarborUsername, d.Secret.HarborPassword)
resp, err := client.Do(req)
if err != nil {
log.Error().Msg(err.Error())
return false
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
log.Error().Msgf("failed to authenticate, response: %+v", resp)
}
return resp.StatusCode == http.StatusOK
}
func (d *RegistryHarbor) EncryptSecret(aes encryption.AES) error {
var err error
d.Secret.HarborPassword, err = aes.Encrypt(d.Secret.HarborPassword)
return err
}
func (d *RegistryHarbor) DecryptSecret(aes encryption.AES) error {
var err error
d.Secret.HarborPassword, err = aes.Decrypt(d.Secret.HarborPassword)
return err
}
func (d *RegistryHarbor) EncryptExtras(aes encryption.AES) error {
return nil
}
func (d *RegistryHarbor) DecryptExtras(aes encryption.AES) error {
return nil
}
func (d *RegistryHarbor) FetchImagesFromRegistry(ctx context.Context) ([]model.IngestedContainerImage, error) {
_, span := telemetry.NewSpan(ctx, "registry", "fetch-images-from-registry")
defer span.End()
return listImages(d.NonSecret.HarborRegistryURL, d.NonSecret.HarborProjectName,
d.NonSecret.HarborUsername, d.Secret.HarborPassword)
}
// getters
func (d *RegistryHarbor) GetSecret() map[string]interface{} {
var secret map[string]interface{}
b, err := json.Marshal(d.Secret)
if err != nil {
log.Error().Msg(err.Error())
return secret
}
if err := json.Unmarshal(b, &secret); err != nil {
log.Error().Msg(err.Error())
return secret
}
return secret
}
func (d *RegistryHarbor) GetExtras() map[string]interface{} {
return map[string]interface{}{}
}
func (d *RegistryHarbor) GetNamespace() string {
return d.NonSecret.HarborProjectName
}
func (d *RegistryHarbor) GetRegistryType() string {
return d.RegistryType
}
func (d *RegistryHarbor) GetUsername() string {
return d.NonSecret.HarborUsername
}