This repository has been archived by the owner on May 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
retriever_cce.go
143 lines (130 loc) · 3.65 KB
/
retriever_cce.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package auth
import (
"crypto/tls"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strings"
"time"
)
//CCERetriever queries auth info from CCE
type CCERetriever struct {
Client *http.Client
ServiceAccountPath string
EnvIdentifiers []string
}
//NewCCERetriever news CCERetriever
func NewCCERetriever() *CCERetriever {
q := CCERetriever{}
q.ServiceAccountPath = ServiceAccountPath
trTLS := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
// https, verify peer: false
q.Client = &http.Client{
Transport: trTLS,
Timeout: 60 * time.Second,
}
q.EnvIdentifiers = make([]string, 0)
for _, v := range CCEEnvIdentifications {
q.EnvIdentifiers = append(q.EnvIdentifiers, v)
}
return &q
}
//Name implements Retriever.Name
func (q *CCERetriever) Name() string {
return "CCE"
}
func (q *CCERetriever) isInCCE() bool {
if len(q.EnvIdentifiers) == 0 {
return false
}
for _, k := range q.EnvIdentifiers {
if v := os.Getenv(k); v != "" {
return true
}
}
return false
}
//GetAuthInfo implements Retriever.GetAuthInfo
func (q *CCERetriever) GetAuthInfo() (string, string, string, error) {
if !q.isInCCE() {
return "", "", "", ErrAuthConfNotExist
}
fd, err := ioutil.ReadFile(filepath.Join(q.ServiceAccountPath, "namespace"))
if err != nil {
return "", "", "", err
}
namespace := strings.TrimSpace(string(fd))
url := fmt.Sprintf("https://%s:%s%s",
os.Getenv(EnvKubernetesServiceHost),
os.Getenv(EnvKubernetesServicePort),
q.API4ImagePullSecret(namespace))
fd, err = ioutil.ReadFile(filepath.Join(q.ServiceAccountPath, "token"))
if err != nil {
return "", "", "", err
}
token := strings.TrimSpace(string(fd))
httpHeaders := http.Header{
HeaderAuthorization: []string{"bearer " + token},
}
// kube secrets json
var kubeSecretsJSONBytes []byte
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", "", "", err
}
req.Header = httpHeaders
resp, err := q.Client.Do(req)
if err != nil {
return "", "", "", err
}
defer resp.Body.Close()
if !(resp.StatusCode >= 200 && resp.StatusCode < 300) {
d, e := ioutil.ReadAll(resp.Body)
if e != nil {
return "", "", "", e
}
return "", "", "", fmt.Errorf("request failed, status: %s, resp: %s", resp.Status, string(d))
}
kubeSecretsJSONBytes, err = ioutil.ReadAll(resp.Body)
if err != nil {
return "", "", "", err
}
// kube secrets
var kubeSecrets KubeSecrets
err = json.Unmarshal(kubeSecretsJSONBytes, &kubeSecrets)
if err != nil {
return "", "", "", err
}
if kubeSecrets.Data.DockerConfigJSON == "" {
return "", "", "", errors.New("dockerConfigJson is empty")
}
// docker config json
dockerConfigJSONBase64Bytes, err := base64.StdEncoding.DecodeString(kubeSecrets.Data.DockerConfigJSON)
if err != nil {
return "", "", "", err
}
return parseAuthInfo(dockerConfigJSONBase64Bytes)
}