forked from kyma-project/kyma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
csrinfohandler.go
executable file
·80 lines (63 loc) · 2.27 KB
/
csrinfohandler.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
package externalapi
import (
"fmt"
"net/http"
"github.com/kyma-project/kyma/components/connector-service/internal/clientcontext"
"github.com/kyma-project/kyma/components/connector-service/internal/httphelpers"
"github.com/kyma-project/kyma/components/connector-service/internal/certificates"
"github.com/kyma-project/kyma/components/connector-service/internal/tokens"
)
const (
TokenFormat = "?token=%s"
CertsEndpoint = "/certificates"
)
type csrInfoHandler struct {
tokenManager tokens.Creator
connectorClientExtractor clientcontext.ConnectorClientExtractor
getInfoURL string
baseURL string
csrSubject certificates.CSRSubject
}
func NewCSRInfoHandler(tokenManager tokens.Creator, connectorClientExtractor clientcontext.ConnectorClientExtractor, getInfoURL string, baseURL string) CSRInfoHandler {
return &csrInfoHandler{
tokenManager: tokenManager,
connectorClientExtractor: connectorClientExtractor,
getInfoURL: getInfoURL,
baseURL: baseURL,
}
}
func (ih *csrInfoHandler) GetCSRInfo(w http.ResponseWriter, r *http.Request) {
clientContextService, err := ih.connectorClientExtractor(r.Context())
if err != nil {
httphelpers.RespondWithErrorAndLog(w, err)
return
}
newToken, err := ih.tokenManager.Save(clientContextService.ClientContext())
if err != nil {
httphelpers.RespondWithErrorAndLog(w, err)
return
}
apiURLs := ih.makeApiURLs(clientContextService)
csrURL := ih.makeCSRURLs(newToken)
certInfo := makeCertInfo(clientContextService.GetSubject().ToString())
httphelpers.RespondWithBody(w, http.StatusOK, csrInfoResponse{CsrURL: csrURL, API: apiURLs, CertificateInfo: certInfo})
}
func (ih *csrInfoHandler) makeCSRURLs(newToken string) string {
csrURL := ih.baseURL + CertsEndpoint
tokenParam := fmt.Sprintf(TokenFormat, newToken)
return csrURL + tokenParam
}
func (ih *csrInfoHandler) makeApiURLs(clientContextService clientcontext.ClientCertContextService) api {
return api{
CertificatesURL: ih.baseURL + CertsEndpoint,
InfoURL: ih.getInfoURL,
RuntimeURLs: clientContextService.GetRuntimeUrls(),
}
}
func makeCertInfo(subject string) certInfo {
return certInfo{
Subject: subject,
Extensions: "",
KeyAlgorithm: "rsa2048",
}
}