This repository has been archived by the owner on Dec 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
154 lines (126 loc) · 4.02 KB
/
client.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
/*
* Copyright 2019, authproxy authors
*
* Licensed 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 client
import (
"context"
"crypto/tls"
"crypto/x509"
"errors"
"fmt"
v1 "github.com/cbrgm/authproxy/client/v1"
"io/ioutil"
"net/http"
)
// ClientSet represents a v1 authproxy client
type ClientSet interface {
Login(username, password string) (string, error)
Authenticate(bearerToken string) (*v1.TokenReviewRequest, error)
}
// AuthClientConfig represents the clientSet configuration
type AuthClientConfig struct {
Path string
CA string
}
// clientSet represents the v1 authproxy client implementation
type clientSet struct {
client *v1.APIClient
}
// newClientV1ForConfig returns a new v1 client for a given config
func NewForConfig(c *AuthClientConfig) (ClientSet, error) {
if c.CA == "" {
return nil, errors.New("invalid config: required authproxy ca is missing")
}
// try to load the ca file
caPool, err := LoadCAFile(c.CA)
if err != nil {
return nil, err
}
// Trust the augmented cert pool in our client
tlsConfig := &tls.Config{
InsecureSkipVerify: false,
RootCAs: caPool,
}
tr := &http.Transport{TLSClientConfig: tlsConfig}
client := &http.Client{Transport: tr}
// create a new clientSet using tls and basepath
config := v1.NewConfiguration()
config.HTTPClient = client
swg := v1.NewAPIClient(config)
if c.Path == "" {
swg.ChangeBasePath("https://localhost:6660/v1")
} else {
swg.ChangeBasePath(c.Path)
}
cl := clientSet{client: swg}
var res ClientSet = &cl
return res, nil
}
// Login logs is a user or an application by username and password.
// It will return a bearer token that can be used to authorize actions performed by he client
func (c *clientSet) Login(username string, password string) (string, error) {
if username == "" || password == "" {
return "", errors.New("invalid arguments: username or password is empty")
}
auth := context.WithValue(context.Background(), v1.ContextBasicAuth, v1.BasicAuth{
UserName: username,
Password: password,
})
tokenReview, resp, err := c.client.AuthApi.Login(auth)
if err != nil {
return "", err
}
if resp.StatusCode == 500 {
return "", errors.New("internal server error: authentication process failed on remote server")
}
if resp.StatusCode == 401 || tokenReview.Status.Authenticated == false {
return "", errors.New("unauthorized: invalid authentication credentials")
}
return tokenReview.Spec.Token, nil
}
// Authenticate authenticates actions performed by the client
// It will return true, if the client in authenticated, false if not
func (c *clientSet) Authenticate(bearerToken string) (*v1.TokenReviewRequest, error) {
if bearerToken == "" {
return nil, errors.New("invalid arguments: token is missing")
}
tokenReview, resp, err := c.client.AuthApi.Authenticate(context.TODO(), v1.TokenReviewRequest{
ApiVersion: "authentication.k8s.io/v1beta1",
Kind: "TokenReview",
Spec: &v1.TokenReviewSpec{
Token: bearerToken,
},
})
if err != nil {
return nil, err
}
if resp.StatusCode != 200 || tokenReview.Status.Authenticated == false {
return &tokenReview, nil
}
return &tokenReview, nil
}
// LoadCAFile loads a single PEM-encoded file from the path specified.
func LoadCAFile(caFile string) (*x509.CertPool, error) {
pool := x509.NewCertPool()
pem, err := ioutil.ReadFile(caFile)
if err != nil {
return nil, fmt.Errorf("Error loading CA File: %s", err)
}
ok := pool.AppendCertsFromPEM(pem)
if !ok {
return nil, fmt.Errorf("Error loading CA File: Couldn't parse PEM in: %s", caFile)
}
return pool, nil
}