X Tutup
Skip to content

Commit 8272035

Browse files
authored
Merge pull request cli#4534 from lpessoa/lp-secret-load-env
Adding set secrets from env files Closes cli#3610
2 parents ca25026 + 95a71f7 commit 8272035

File tree

5 files changed

+330
-133
lines changed

5 files changed

+330
-133
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,11 @@ require (
1717
github.com/google/go-cmp v0.5.6
1818
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
1919
github.com/gorilla/websocket v1.4.2
20+
github.com/hashicorp/go-multierror v1.1.1
2021
github.com/hashicorp/go-version v1.3.0
2122
github.com/henvic/httpretty v0.0.6
2223
github.com/itchyny/gojq v0.12.6
24+
github.com/joho/godotenv v1.4.0
2325
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
2426
github.com/mattn/go-colorable v0.1.12
2527
github.com/mattn/go-isatty v0.0.14

go.sum

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/ad
243243
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
244244
github.com/hashicorp/consul/api v1.11.0/go.mod h1:XjsvQN+RJGWI2TWy1/kqaE16HrR2J/FWgkYjdZQsX9M=
245245
github.com/hashicorp/consul/sdk v0.8.0/go.mod h1:GBvyrGALthsZObzUGsfgHZQDXjg4lOjagTIwIR1vPms=
246+
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
246247
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
247248
github.com/hashicorp/go-cleanhttp v0.5.0/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
248249
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
@@ -254,6 +255,8 @@ github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjh
254255
github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM=
255256
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
256257
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
258+
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
259+
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
257260
github.com/hashicorp/go-retryablehttp v0.5.3/go.mod h1:9B5zBasrRhHXnJnui7y6sL7es7NDiJgTc6Er0maI1Xs=
258261
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
259262
github.com/hashicorp/go-sockaddr v1.0.0/go.mod h1:7Xibr9yA9JjQq1JpNB2Vw7kxv8xerXegt+ozgdvDeDU=
@@ -286,6 +289,8 @@ github.com/itchyny/gojq v0.12.6 h1:VjaFn59Em2wTxDNGcrRkDK9ZHMNa8IksOgL13sLL4d0=
286289
github.com/itchyny/gojq v0.12.6/go.mod h1:ZHrkfu7A+RbZLy5J1/JKpS4poEqrzItSTGDItqsfP0A=
287290
github.com/itchyny/timefmt-go v0.1.3 h1:7M3LGVDsqcd0VZH2U+x393obrzZisp7C0uEe921iRkU=
288291
github.com/itchyny/timefmt-go v0.1.3/go.mod h1:0osSSCQSASBJMsIZnhAaF1C2fCBTJZXrnj37mG8/c+A=
292+
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
293+
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
289294
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
290295
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
291296
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=

pkg/cmd/secret/set/http.go

Lines changed: 12 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package set
22

33
import (
44
"bytes"
5-
"encoding/base64"
65
"encoding/json"
76
"fmt"
87
"sort"
@@ -11,14 +10,13 @@ import (
1110

1211
"github.com/cli/cli/v2/api"
1312
"github.com/cli/cli/v2/internal/ghrepo"
14-
"github.com/cli/cli/v2/pkg/cmd/secret/shared"
1513
)
1614

1715
type SecretPayload struct {
18-
EncryptedValue string `json:"encrypted_value"`
19-
Visibility string `json:"visibility,omitempty"`
20-
Repositories []int `json:"selected_repository_ids,omitempty"`
21-
KeyID string `json:"key_id"`
16+
EncryptedValue string `json:"encrypted_value"`
17+
Visibility string `json:"visibility,omitempty"`
18+
Repositories []int64 `json:"selected_repository_ids,omitempty"`
19+
KeyID string `json:"key_id"`
2220
}
2321

2422
// The Codespaces Secret API currently expects repositories IDs as strings
@@ -29,7 +27,6 @@ type CodespacesSecretPayload struct {
2927
}
3028

3129
type PubKey struct {
32-
Raw [32]byte
3330
ID string `json:"key_id"`
3431
Key string
3532
}
@@ -40,17 +37,6 @@ func getPubKey(client *api.Client, host, path string) (*PubKey, error) {
4037
if err != nil {
4138
return nil, err
4239
}
43-
44-
if pk.Key == "" {
45-
return nil, fmt.Errorf("failed to find public key at %s/%s", host, path)
46-
}
47-
48-
decoded, err := base64.StdEncoding.DecodeString(pk.Key)
49-
if err != nil {
50-
return nil, fmt.Errorf("failed to decode public key: %w", err)
51-
}
52-
53-
copy(pk.Raw[:], decoded[0:32])
5440
return &pk, nil
5541
}
5642

@@ -82,33 +68,7 @@ func putSecret(client *api.Client, host, path string, payload interface{}) error
8268
return client.REST(host, "PUT", path, requestBody, nil)
8369
}
8470

85-
func putOrgSecret(client *api.Client, host string, pk *PubKey, opts SetOptions, eValue string) error {
86-
secretName := opts.SecretName
87-
orgName := opts.OrgName
88-
visibility := opts.Visibility
89-
90-
var repositoryIDs []int
91-
var err error
92-
if orgName != "" && visibility == shared.Selected {
93-
repos := make([]ghrepo.Interface, 0, len(opts.RepositoryNames))
94-
for _, repositoryName := range opts.RepositoryNames {
95-
var repo ghrepo.Interface
96-
if strings.Contains(repositoryName, "/") {
97-
repo, err = ghrepo.FromFullNameWithHost(repositoryName, host)
98-
if err != nil {
99-
return fmt.Errorf("invalid repository name: %w", err)
100-
}
101-
} else {
102-
repo = ghrepo.NewWithHost(opts.OrgName, repositoryName, host)
103-
}
104-
repos = append(repos, repo)
105-
}
106-
repositoryIDs, err = mapRepoToID(client, host, repos)
107-
if err != nil {
108-
return fmt.Errorf("failed to look up IDs for repositories %v: %w", opts.RepositoryNames, err)
109-
}
110-
}
111-
71+
func putOrgSecret(client *api.Client, host string, pk *PubKey, orgName, visibility, secretName, eValue string, repositoryIDs []int64) error {
11272
payload := SecretPayload{
11373
EncryptedValue: eValue,
11474
KeyID: pk.ID,
@@ -120,35 +80,21 @@ func putOrgSecret(client *api.Client, host string, pk *PubKey, opts SetOptions,
12080
return putSecret(client, host, path, payload)
12181
}
12282

123-
func putUserSecret(client *api.Client, host string, pk *PubKey, opts SetOptions, eValue string) error {
83+
func putUserSecret(client *api.Client, host string, pk *PubKey, key, eValue string, repositoryIDs []int64) error {
12484
payload := CodespacesSecretPayload{
12585
EncryptedValue: eValue,
12686
KeyID: pk.ID,
12787
}
12888

129-
if len(opts.RepositoryNames) > 0 {
130-
repos := make([]ghrepo.Interface, len(opts.RepositoryNames))
131-
for i, repo := range opts.RepositoryNames {
132-
// For user secrets, repository names should be fully qualifed (e.g. "owner/repo")
133-
repoNWO, err := ghrepo.FromFullNameWithHost(repo, host)
134-
if err != nil {
135-
return err
136-
}
137-
repos[i] = repoNWO
138-
}
139-
140-
repositoryIDs, err := mapRepoToID(client, host, repos)
141-
if err != nil {
142-
return fmt.Errorf("failed to look up repository IDs: %w", err)
143-
}
89+
if len(repositoryIDs) > 0 {
14490
repositoryStringIDs := make([]string, len(repositoryIDs))
14591
for i, id := range repositoryIDs {
146-
repositoryStringIDs[i] = strconv.Itoa(id)
92+
repositoryStringIDs[i] = strconv.FormatInt(id, 10)
14793
}
14894
payload.Repositories = repositoryStringIDs
14995
}
15096

151-
path := fmt.Sprintf("user/codespaces/secrets/%s", opts.SecretName)
97+
path := fmt.Sprintf("user/codespaces/secrets/%s", key)
15298
return putSecret(client, host, path, payload)
15399
}
154100

@@ -171,7 +117,7 @@ func putRepoSecret(client *api.Client, pk *PubKey, repo ghrepo.Interface, secret
171117
}
172118

173119
// This does similar logic to `api.RepoNetwork`, but without the overfetching.
174-
func mapRepoToID(client *api.Client, host string, repositories []ghrepo.Interface) ([]int, error) {
120+
func mapRepoToID(client *api.Client, host string, repositories []ghrepo.Interface) ([]int64, error) {
175121
queries := make([]string, 0, len(repositories))
176122
for i, repo := range repositories {
177123
queries = append(queries, fmt.Sprintf(`
@@ -184,7 +130,7 @@ func mapRepoToID(client *api.Client, host string, repositories []ghrepo.Interfac
184130
query := fmt.Sprintf(`query MapRepositoryNames { %s }`, strings.Join(queries, ""))
185131

186132
graphqlResult := make(map[string]*struct {
187-
DatabaseID int `json:"databaseId"`
133+
DatabaseID int64 `json:"databaseId"`
188134
})
189135

190136
if err := client.GraphQL(host, query, nil, &graphqlResult); err != nil {
@@ -197,10 +143,9 @@ func mapRepoToID(client *api.Client, host string, repositories []ghrepo.Interfac
197143
}
198144
sort.Strings(repoKeys)
199145

200-
result := make([]int, len(repositories))
146+
result := make([]int64, len(repositories))
201147
for i, k := range repoKeys {
202148
result[i] = graphqlResult[k].DatabaseID
203149
}
204-
205150
return result, nil
206151
}

0 commit comments

Comments
 (0)
X Tutup