This repository has been archived by the owner on Mar 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
manager.go
73 lines (56 loc) · 1.45 KB
/
manager.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
package cosign
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"github.com/sirupsen/logrus"
)
type AcornPublicKey struct {
ID string `json:"id,omitempty"`
Key string `json:"key,omitempty"`
Fingerprint string `json:"fingerprint,omitempty"`
}
func getAcornPublicKeys(username string) ([]AcornPublicKey, error) {
logrus.Debugf("Getting public keys for Acorn user %s", username)
parts := strings.Split(username, "@")
if len(parts) > 2 {
return nil, fmt.Errorf("invalid username: %s", username)
}
host := "https://acorn.io"
if len(parts) == 2 {
username = parts[0]
host = parts[1]
}
url := fmt.Sprintf("%s/v1/manager.acorn.io.publickeys/%s/", host, username)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return nil, fmt.Errorf("failed to get public keys for user %s: %d (%s)", username, resp.StatusCode, resp.Status)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var data struct {
Data []AcornPublicKey `json:"data"`
}
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
keys := data.Data
if len(keys) == 0 {
return nil, fmt.Errorf("no keys found for user %s", username)
}
logrus.Debugf("Found %d keys for user %s in Acorn Manager (%s)", len(keys), username, url)
return keys, nil
}