This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.go
106 lines (94 loc) · 3.44 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
package api
import (
"context"
"crypto/tls"
"fmt"
oauthutils "github.com/keptn/go-utils/pkg/common/oauth2"
"github.com/keptn/go-utils/pkg/sdk/internal/config"
logger "github.com/sirupsen/logrus"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
"net/http"
"time"
)
// CreateClientGetter returns a HTTPClientGetter implementation based on the values certain properties
// inside the given env configuration
func CreateClientGetter(envConfig config.EnvConfig) HTTPClientGetter {
if envConfig.OAuthEnabled() {
logger.Infof("Using Oauth to connect to Keptn wth client ID %s and scopes %v", envConfig.OAuthClientID, envConfig.OAuthScopes)
return NewOauthClientGetter(envConfig, oauthutils.NewOauthDiscovery(&http.Client{}))
}
return New(envConfig)
}
// HTTPClientGetter is responsible for creating an HTTP client
type HTTPClientGetter interface {
// Get Creates the HTTP Client
Get() (*http.Client, error)
}
// OAuthClientGetter creates an HTTP client configured for use with SSO/Oauth
type OAuthClientGetter struct {
*SimpleClientGetter
envConfig config.EnvConfig
oauthDiscovery oauthutils.OauthLocationGetter
}
// NewOauthClientGetter creates a new instance of a OAuthClientGetter
func NewOauthClientGetter(envConfig config.EnvConfig, oauthDiscovery oauthutils.OauthLocationGetter) *OAuthClientGetter {
return &OAuthClientGetter{
SimpleClientGetter: &SimpleClientGetter{envConfig: envConfig},
envConfig: envConfig,
oauthDiscovery: oauthDiscovery,
}
}
func (g *OAuthClientGetter) Get() (*http.Client, error) {
c, err := g.SimpleClientGetter.Get()
if err != nil {
return nil, err
}
if g.envConfig.OAuthClientID == "" || g.envConfig.OAuthClientSecret == "" || len(g.envConfig.OAuthScopes) == 0 {
return nil, fmt.Errorf("client id or client secret or scopes missing")
}
if g.envConfig.OauthTokenURL != "" {
logger.Infof("Using Token URL for Oauth flow: %s", g.envConfig.OauthTokenURL)
conf := clientcredentials.Config{
ClientID: g.envConfig.OAuthClientID,
ClientSecret: g.envConfig.OAuthClientSecret,
Scopes: g.envConfig.OAuthScopes,
TokenURL: g.envConfig.OauthTokenURL,
}
return conf.Client(context.WithValue(context.TODO(), oauth2.HTTPClient, c)), nil
}
if g.envConfig.OAuthDiscovery != "" {
logger.Infof("Using Discovery URL for Oauth flow: %s", g.envConfig.OAuthDiscovery)
ctx, cancel := context.WithTimeout(context.TODO(), time.Second*10)
defer cancel()
discoveryRes, err := g.oauthDiscovery.Discover(ctx, g.envConfig.OAuthDiscovery)
if err != nil {
return nil, err
}
conf := clientcredentials.Config{
ClientID: g.envConfig.OAuthClientID,
ClientSecret: g.envConfig.OAuthClientSecret,
Scopes: g.envConfig.OAuthScopes,
TokenURL: discoveryRes.TokenEndpoint,
}
return conf.Client(context.WithValue(context.TODO(), oauth2.HTTPClient, c)), nil
}
return nil, fmt.Errorf("no discovery or token url is provided")
}
// SimpleClientGetter creates a basic HTTP client
type SimpleClientGetter struct {
envConfig config.EnvConfig
}
// New Creates a new instance of a SimpleClientGetter
func New(envConfig config.EnvConfig) *SimpleClientGetter {
return &SimpleClientGetter{envConfig: envConfig}
}
func (g *SimpleClientGetter) Get() (*http.Client, error) {
c := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: !g.envConfig.VerifySSL}, //nolint:gosec
},
Timeout: g.envConfig.GetAPIProxyHTTPTimeout(),
}
return c, nil
}