-
Notifications
You must be signed in to change notification settings - Fork 6
/
harbor.go
172 lines (162 loc) · 4.21 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package harbor
import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/cnrancher/hangar/pkg/utils"
"github.com/containers/image/v5/types"
"github.com/sirupsen/logrus"
)
var (
ErrRegistryIsNotHarbor = errors.New("registry server is not harbor V2")
)
func GetURL(
ctx context.Context,
registry string,
tlsVerify bool,
) (string, error) {
registry = strings.TrimSuffix(registry, "/")
u := fmt.Sprintf("%s/api/v2.0/ping", registry)
// Detect registry is using HTTPS protocol or not.
detectURL, resp, err := utils.DetectURL(ctx, u, !tlsVerify)
if err != nil {
return "", fmt.Errorf("harbor.GetURL: %w", err)
}
if resp == nil {
return "", ErrRegistryIsNotHarbor
}
var ubase string
if strings.HasPrefix(detectURL, "https://") {
ubase = fmt.Sprintf("https://%s", registry)
} else {
ubase = fmt.Sprintf("http://%s", registry)
}
switch resp.StatusCode {
case http.StatusOK:
b, _ := io.ReadAll(resp.Body)
if len(b) > 0 {
if len(b) > 20 {
b = b[:20]
}
logrus.Debugf("server response: %v", string(b))
content := strings.ToLower(string(b))
if strings.Contains(content, "pong") {
return ubase, nil
}
}
}
return "", ErrRegistryIsNotHarbor
}
// ProjectExists check project exists or not on harbor v2.
func ProjectExists(
ctx context.Context,
name, u string,
credential *types.DockerAuthConfig,
tlsVerify bool,
) (bool, error) {
client := &http.Client{
Timeout: time.Second * 5,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !tlsVerify},
Proxy: http.ProxyFromEnvironment,
},
}
u = strings.TrimSuffix(u, "/")
u = fmt.Sprintf("%s/api/v2.0/projects?project_name=%s", u, url.QueryEscape(name))
r, err := http.NewRequestWithContext(ctx, http.MethodHead, u, nil)
if err != nil {
return false, fmt.Errorf("harbor.ProjectExists: %w", err)
}
auth := fmt.Sprintf("%s:%s", credential.Username, credential.Password)
r.Header.Add("Authorization", "Basic "+utils.Base64(auth))
r.Header.Add("Accept", "application/json")
resp, err := utils.HTTPClientDoWithRetry(ctx, client, r)
if err != nil {
return false, fmt.Errorf("harbor.ProjectExists: %w", err)
}
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
switch resp.StatusCode {
case http.StatusOK:
logrus.Debugf("harbor project %q already exists", name)
return true, nil
case http.StatusNotFound:
logrus.Debugf("harbor project %q not found", name)
default:
return false, fmt.Errorf("harbor.ProjectExists: %q response: %v",
u, resp.Status)
}
return false, nil
}
// CreateProject creates project for harbor v2
func CreateProject(
ctx context.Context,
name, u string,
credential *types.DockerAuthConfig,
tlsVerify bool,
) error {
data := struct {
ProjectName string `json:"project_name"`
Metadata struct {
Public string `json:"public"`
} `json:"metadata"`
}{
ProjectName: name,
Metadata: struct {
Public string `json:"public"`
}{
Public: "false",
},
}
b, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("harbor.CreateHarborProject: json.Marshal: %w", err)
}
client := &http.Client{
Timeout: time.Second * 5,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !tlsVerify},
Proxy: http.ProxyFromEnvironment,
},
}
u = strings.TrimSuffix(u, "/")
u = fmt.Sprintf("%s/api/v2.0/projects", u)
r, err := http.NewRequestWithContext(
ctx, http.MethodPost, u, bytes.NewReader(b))
if err != nil {
return fmt.Errorf("harbor.CreateProject: %w", err)
}
auth := fmt.Sprintf("%s:%s", credential.Username, credential.Password)
r.Header.Add("Authorization", "Basic "+utils.Base64(auth))
r.Header.Add("Content-Type", "application/json")
resp, err := utils.HTTPClientDoWithRetry(ctx, client, r)
if err != nil {
return fmt.Errorf("harbor.CreateProject: %w", err)
}
defer func() {
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
}()
switch resp.StatusCode {
case http.StatusCreated:
case http.StatusConflict:
logrus.Debugf("already created project %q, response: %s",
name, resp.Status)
default:
return fmt.Errorf("failed to create project %q, response: %s",
name, resp.Status)
}
return nil
}