-
Notifications
You must be signed in to change notification settings - Fork 584
/
acr.go
113 lines (93 loc) · 2.73 KB
/
acr.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
package acr
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/deepfence/ThreatMapper/deepfence_server/model"
"github.com/deepfence/ThreatMapper/deepfence_server/pkg/constants"
"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) (*RegistryACR, error) {
var r RegistryACR
err := json.Unmarshal(data, &r)
if err != nil {
return nil, err
}
return &r, nil
}
func (d *RegistryACR) ValidateFields(v *validator.Validate) error {
return v.Struct(d)
}
func (d *RegistryACR) IsValidCredential() bool {
if d.NonSecret.AzureRegistryURL == "" {
return false
}
listRepos := "%s/v2/_catalog"
url := fmt.Sprintf(listRepos, d.NonSecret.AzureRegistryURL)
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.AzureRegistryUsername, d.Secret.AzureRegistryPassword)
resp, err := client.Do(req)
if err != nil {
log.Error().Msg(err.Error())
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}
func (d *RegistryACR) EncryptSecret(aes encryption.AES) error {
var err error
d.Secret.AzureRegistryPassword, err = aes.Encrypt(d.Secret.AzureRegistryPassword)
return err
}
func (d *RegistryACR) DecryptSecret(aes encryption.AES) error {
var err error
d.Secret.AzureRegistryPassword, err = aes.Decrypt(d.Secret.AzureRegistryPassword)
return err
}
func (d *RegistryACR) EncryptExtras(aes encryption.AES) error {
return nil
}
func (d *RegistryACR) DecryptExtras(aes encryption.AES) error {
return nil
}
func (d *RegistryACR) FetchImagesFromRegistry(ctx context.Context) ([]model.IngestedContainerImage, error) {
_, span := telemetry.NewSpan(ctx, "registry", "fetch-images-from-registry")
defer span.End()
return listImagesRegistryV2(d.NonSecret.AzureRegistryURL, "",
d.NonSecret.AzureRegistryUsername, d.Secret.AzureRegistryPassword)
}
// getters
func (d *RegistryACR) 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 *RegistryACR) GetExtras() map[string]interface{} {
return map[string]interface{}{}
}
func (d *RegistryACR) GetNamespace() string {
return d.NonSecret.AzureRegistryUsername
}
func (d *RegistryACR) GetRegistryType() string {
return constants.ACR
}
func (d *RegistryACR) GetUsername() string {
return ""
}