Skip to content

Commit e753266

Browse files
committed
Implement enough to satisfy the tests TestPingTPP TestRetrieveSelfIdentity TestGetRefreshToken TestGetRefreshTokenWithDefaultScope
/vedauth/authorize/oauth /vedsdk/Identity/Self /vedsdk/certificates/checkpolicy /vedsdk/ Signed-off-by: Richard Wall <[email protected]>
1 parent 896851a commit e753266

25 files changed

+3321
-34
lines changed

Makefile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,16 @@ test: get linter
5959
go test -v -coverprofile=cov_cmd.out ./cmd/vcert
6060
go tool cover -func=cov_cmd.out
6161

62+
WHAT ?= .
63+
6264
tpp_test: get
63-
go test -v $(GOFLAGS) -coverprofile=cov_tpp.out ./pkg/venafi/tpp
65+
go test -v $(GOFLAGS) -coverprofile=cov_tpp.out ./pkg/venafi/tpp -run $(WHAT)
6466
go tool cover -func=cov_tpp.out
6567

6668
fake_tpp_test: export MAKE := $(MAKE)
69+
fake_tpp_test: export WHAT := ^\(TestPingTPP\|TestRetrieveSelfIdentity\|TestGetRefreshToken\|TestGetRefreshTokenWithDefaultScope\)
6770
fake_tpp_test:
68-
go test ./test/tpp/fake/...
71+
go test ./test/tpp/fake/... -count=1 -v
6972

7073
cloud_test: get
7174
go test -v $(GOFLAGS) -coverprofile=cov_vaas.out ./pkg/venafi/cloud

go.mod

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@ module github.com/Venafi/vcert/v4
22

33
require (
44
github.com/go-logr/logr v1.2.3
5+
github.com/go-openapi/errors v0.20.3
6+
github.com/go-openapi/strfmt v0.21.3
7+
github.com/go-openapi/swag v0.22.3
58
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c
69
github.com/pavel-v-chernykh/keystore-go/v4 v4.1.0
710
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d
811
github.com/spf13/viper v1.7.0
9-
github.com/stretchr/testify v1.3.0
12+
github.com/stretchr/testify v1.8.0
1013
github.com/urfave/cli/v2 v2.1.1
1114
github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a
12-
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519
13-
golang.org/x/sync v0.0.0-20190423024810-112230192c58
15+
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d
16+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
1417
gopkg.in/ini.v1 v1.51.0
1518
gopkg.in/yaml.v2 v2.4.0
1619
software.sslmate.com/src/go-pkcs12 v0.0.0-20180114231543-2291e8f0f237

go.sum

Lines changed: 61 additions & 11 deletions
Large diffs are not rendered by default.

pkg/venafi/tpp/tpp.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ type oauthGetRefreshTokenRequest struct {
198198
}
199199
type OauthGetRefreshTokenResponse struct {
200200
Access_token string `json:"access_token,omitempty"`
201-
Expires int `json:"expires,omitempty"`
202-
ExpiresIn int `json:"expires_in,omitempty"` //Attribute added as it's used on vSSH
201+
Expires uint64 `json:"expires,omitempty"`
202+
ExpiresIn uint64 `json:"expires_in,omitempty"` //Attribute added as it's used on vSSH
203203
Identity string `json:"identity,omitempty"`
204204
Refresh_token string `json:"refresh_token,omitempty"`
205-
Refresh_until int `json:"refresh_until,omitempty"`
205+
Refresh_until uint64 `json:"refresh_until,omitempty"`
206206
Scope string `json:"scope,omitempty"`
207207
Token_type string `json:"token_type,omitempty"`
208208
}
@@ -461,7 +461,7 @@ func (c *Connector) request(method string, resource urlResource, data interface{
461461
defer res.Body.Close()
462462
body, err = ioutil.ReadAll(res.Body)
463463
// Do not enable trace in production
464-
trace := false // IMPORTANT: sensitive information can be diclosured
464+
trace := true // IMPORTANT: sensitive information can be diclosured
465465
// I hope you know what are you doing
466466
if trace {
467467
log.Println("#################")

test/tpp/fake/fake.go

Lines changed: 142 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,57 @@ package fake
22

33
import (
44
"context"
5+
"encoding/json"
56
"net/http"
67
"net/http/httptest"
8+
"sync"
9+
"time"
710

11+
"github.com/Venafi/vcert/v4/test/tpp/fake/models"
812
"github.com/go-logr/logr"
913
)
1014

11-
type Fake struct {
12-
*httptest.Server
15+
type state struct {
16+
sync.RWMutex
17+
username string
18+
password string
19+
refreshToken string
20+
accessToken string
21+
refreshTokenExpires time.Time
1322
}
1423

15-
func New() *Fake {
16-
mux := http.NewServeMux()
17-
mux.HandleFunc("/vedauth/authorize/oauth", func(w http.ResponseWriter, req *http.Request) {
18-
defer req.Body.Close()
19-
w.Write([]byte("{}"))
20-
return
21-
})
22-
ts := httptest.NewUnstartedServer(mux)
23-
return &Fake{
24-
Server: ts,
25-
}
24+
func (o *state) WithUsername(username string) *state {
25+
o.Lock()
26+
defer o.Unlock()
27+
o.username = username
28+
return o
29+
}
30+
31+
func (o *state) WithPassword(password string) *state {
32+
o.Lock()
33+
defer o.Unlock()
34+
o.password = password
35+
return o
36+
}
37+
38+
func (o *state) WithRefreshToken(token string) *state {
39+
o.Lock()
40+
defer o.Unlock()
41+
o.accessToken = token
42+
return o
43+
}
44+
45+
func (o *state) WithAccessToken(token string) *state {
46+
o.Lock()
47+
defer o.Unlock()
48+
o.accessToken = token
49+
return o
50+
}
51+
52+
type Fake struct {
53+
*state
54+
*httptest.Server
55+
log logr.Logger
2656
}
2757

2858
func (o *Fake) Start(ctx context.Context) {
@@ -37,6 +67,105 @@ func (o *Fake) Close(ctx context.Context) {
3767
o.Server.Close()
3868
}
3969

70+
func New(log logr.Logger) *Fake {
71+
mux := http.NewServeMux()
72+
ts := httptest.NewUnstartedServer(mux)
73+
f := &Fake{
74+
log: log,
75+
state: &state{},
76+
Server: ts,
77+
}
78+
mux.HandleFunc("/vedauth/authorize/oauth", f.handlerAuthorizeOAuth)
79+
mux.HandleFunc("/vedsdk/Identity/Self", f.handlerIdentitySelf)
80+
mux.HandleFunc("/vedsdk/certificates/checkpolicy", f.handlerCertificatesCheckPolicy)
81+
mux.HandleFunc("/vedsdk/", f.handlerPing)
82+
mux.HandleFunc("/", f.handlerCatchAll)
83+
return f
84+
}
85+
86+
func (o *Fake) handlerAuthorizeOAuth(w http.ResponseWriter, req *http.Request) {
87+
defer req.Body.Close()
88+
o.log.Info("request", "uri", req.RequestURI)
89+
decoder := json.NewDecoder(req.Body)
90+
var in models.AuthorizeOAuthRequest
91+
if err := decoder.Decode(&in); err != nil {
92+
http.Error(w, err.Error(), http.StatusInternalServerError)
93+
return
94+
}
95+
if in.Username != o.username || in.Password != o.password {
96+
// Mimics the behavior of TPP 20.4 and above. See:
97+
// https://github.com/jetstack/venafi-oauth-helper/issues/25#issuecomment-854037706
98+
http.Error(w, `{"error":"invalid_grant","error_description":"Username\/password combination not valid"}`,
99+
http.StatusBadRequest)
100+
return
101+
}
102+
o.WithRefreshToken(o.refreshToken + "x")
103+
o.WithAccessToken(o.accessToken + "x")
104+
out := models.AuthorizeOAuthResponse{
105+
AccessToken: o.accessToken,
106+
Expires: uint64(time.Now().UTC().Add(time.Hour).Unix()),
107+
RefreshToken: o.refreshToken,
108+
RefreshUntil: uint64(o.refreshTokenExpires.Unix()),
109+
Scope: in.Scope,
110+
TokenType: "Bearer",
111+
Identity: "",
112+
}
113+
encoder := json.NewEncoder(w)
114+
if err := encoder.Encode(&out); err != nil {
115+
http.Error(w, err.Error(), http.StatusInternalServerError)
116+
return
117+
}
118+
}
119+
120+
func (o *Fake) handlerIdentitySelf(w http.ResponseWriter, req *http.Request) {
121+
defer req.Body.Close()
122+
log := o.log.WithValues("uri", req.RequestURI).WithName("handlerIdentifySelf")
123+
log.V(1).Info("request")
124+
out := models.IdentityWebResponse{
125+
Identities: []*models.IdentityEntry{
126+
&models.IdentityEntry{
127+
Name: "Joe Bloggs",
128+
},
129+
},
130+
}
131+
encoder := json.NewEncoder(w)
132+
if err := encoder.Encode(&out); err != nil {
133+
log.Error(err, "While encoding response")
134+
http.Error(w, err.Error(), http.StatusInternalServerError)
135+
return
136+
}
137+
}
138+
139+
func (o *Fake) handlerCertificatesCheckPolicy(w http.ResponseWriter, req *http.Request) {
140+
defer req.Body.Close()
141+
log := o.log.WithValues("uri", req.RequestURI).WithName("handlerCertificatesCheckPolicy")
142+
log.V(1).Info("request")
143+
decoder := json.NewDecoder(req.Body)
144+
var in models.CheckPolicyRequest
145+
if err := decoder.Decode(&in); err != nil {
146+
http.Error(w, err.Error(), http.StatusInternalServerError)
147+
return
148+
}
149+
out := models.CheckPolicyResponse{}
150+
encoder := json.NewEncoder(w)
151+
if err := encoder.Encode(&out); err != nil {
152+
http.Error(w, err.Error(), http.StatusInternalServerError)
153+
return
154+
}
155+
}
156+
157+
func (o *Fake) handlerPing(w http.ResponseWriter, req *http.Request) {
158+
defer req.Body.Close()
159+
o.log.Info("request", "uri", req.RequestURI)
160+
if req.URL.Path != "/vedsdk/" {
161+
panic(req)
162+
}
163+
}
164+
165+
func (o *Fake) handlerCatchAll(w http.ResponseWriter, req *http.Request) {
166+
panic(req)
167+
}
168+
40169
func logFromContext(ctx context.Context) logr.Logger {
41170
log, err := logr.FromContext(ctx)
42171
if err != nil {

test/tpp/fake/fake_test.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ func TestFake(t *testing.T) {
3030
})
3131
ctx = logr.NewContext(ctx, log)
3232

33-
s := fake.New()
33+
const (
34+
tppUsername = "user1"
35+
tppPassword = "password1"
36+
tppZone = "zone1"
37+
)
38+
s := fake.New(log)
39+
s.WithUsername(tppUsername).WithPassword(tppPassword)
3440
s.Start(ctx)
3541
t.Cleanup(func() { s.Close(ctx) })
3642

@@ -39,6 +45,9 @@ func TestFake(t *testing.T) {
3945
cmd.Env = append(
4046
os.Environ(),
4147
"TPP_URL="+s.URL,
48+
"TPP_USER="+tppUsername,
49+
"TPP_PASSWORD="+tppPassword,
50+
"TPP_ZONE="+tppZone,
4251
)
4352
cmd.Stdout = os.Stdout
4453
cmd.Stderr = os.Stderr

test/tpp/fake/models/authorize_o_auth_request.go

Lines changed: 65 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)