/
macaroonsuite.go
211 lines (187 loc) · 5.94 KB
/
macaroonsuite.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package testing
import (
"context"
"net/http"
"net/http/cookiejar"
"net/url"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakery/checkers"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/bakerytest"
"github.com/go-macaroon-bakery/macaroon-bakery/v3/httpbakery"
"github.com/juju/errors"
"github.com/juju/names/v5"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/macaroon.v2"
"github.com/juju/juju/api"
"github.com/juju/juju/controller"
"github.com/juju/juju/core/permission"
jujutesting "github.com/juju/juju/juju/testing"
"github.com/juju/juju/state"
"github.com/juju/juju/testing/factory"
)
// MacaroonSuite wraps a JujuConnSuite with macaroon authentication
// enabled.
type MacaroonSuite struct {
jujutesting.JujuConnSuite
// discharger holds the third-party discharger used
// for authentication.
discharger *bakerytest.Discharger
// DischargerLogin is called by the discharger when an
// API macaroon is discharged. It should either return
// the chosen username or an empty string, in which case
// the discharge is denied.
// If this is nil, func() {return ""} is implied.
DischargerLogin func() string
}
func (s *MacaroonSuite) SetUpTest(c *gc.C) {
s.DischargerLogin = nil
s.discharger = bakerytest.NewDischarger(nil)
s.discharger.CheckerP = httpbakery.ThirdPartyCaveatCheckerPFunc(func(ctx context.Context, p httpbakery.ThirdPartyCaveatCheckerParams) ([]checkers.Caveat, error) {
cond, _, err := checkers.ParseCaveat(string(p.Caveat.Condition))
if err != nil {
return nil, errors.Trace(err)
}
if cond != "is-authenticated-user" {
return nil, errors.New("unknown caveat")
}
var username string
if s.DischargerLogin != nil {
username = s.DischargerLogin()
}
if username == "" {
return nil, errors.New("login denied by discharger")
}
return []checkers.Caveat{checkers.DeclaredCaveat("username", username)}, nil
})
s.JujuConnSuite.ControllerConfigAttrs = map[string]interface{}{
controller.IdentityURL: s.discharger.Location(),
}
s.JujuConnSuite.SetUpTest(c)
}
func (s *MacaroonSuite) TearDownTest(c *gc.C) {
s.discharger.Close()
s.JujuConnSuite.TearDownTest(c)
}
// DischargerLocation returns the URL of the third party caveat
// discharger.
func (s *MacaroonSuite) DischargerLocation() string {
return s.discharger.Location()
}
// AddModelUser is a convenience function that adds an external
// user to the current model. It will panic
// if the user name is local.
func (s *MacaroonSuite) AddModelUser(c *gc.C, username string) {
if names.NewUserTag(username).IsLocal() {
panic("cannot use MacaroonSuite.AddModelUser to add a local name")
}
s.Factory.MakeModelUser(c, &factory.ModelUserParams{
User: username,
})
}
// AddControllerUser is a convenience funcation that adds
// a controller user with the specified access.
func (s *MacaroonSuite) AddControllerUser(c *gc.C, username string, access permission.Access) {
_, err := s.State.AddControllerUser(state.UserAccessSpec{
User: names.NewUserTag(username),
CreatedBy: s.AdminUserTag(c),
Access: access,
})
c.Assert(err, jc.ErrorIsNil)
}
// OpenAPI opens a connection to the API using the given information.
// and empty DialOpts. If info is nil, s.APIInfo(c) is used.
// If jar is non-nil, it will be used as the store for the cookies created
// as a result of API interaction.
func (s *MacaroonSuite) OpenAPI(c *gc.C, info *api.Info, jar http.CookieJar) api.Connection {
if info == nil {
info = s.APIInfo(c)
}
bakeryClient := httpbakery.NewClient()
if jar != nil {
bakeryClient.Client.Jar = jar
}
conn, err := api.Open(info, api.DialOpts{
BakeryClient: bakeryClient,
})
c.Assert(err, gc.IsNil)
return conn
}
// APIInfo returns API connection info suitable for
// connecting to the API using macaroon authentication.
func (s *MacaroonSuite) APIInfo(c *gc.C) *api.Info {
info := s.JujuConnSuite.APIInfo(c)
info.Tag = nil
info.Password = ""
// Fill in any old macaroon to ensure we don't attempt
// an anonymous login.
mac, err := NewMacaroon("test")
c.Assert(err, jc.ErrorIsNil)
info.Macaroons = []macaroon.Slice{{mac}}
return info
}
// NewClearableCookieJar returns a new ClearableCookieJar.
func NewClearableCookieJar() *ClearableCookieJar {
jar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
return &ClearableCookieJar{
jar: jar,
}
}
// ClearableCookieJar implements a cookie jar
// that can be cleared of all cookies for testing purposes.
type ClearableCookieJar struct {
jar http.CookieJar
}
// Clear clears all the cookies in the jar.
// It is not OK to call Clear concurrently
// with the other methods.
func (jar *ClearableCookieJar) Clear() {
newJar, err := cookiejar.New(nil)
if err != nil {
panic(err)
}
jar.jar = newJar
}
// Cookies implements http.CookieJar.Cookies.
func (jar *ClearableCookieJar) Cookies(u *url.URL) []*http.Cookie {
return jar.jar.Cookies(u)
}
// Cookies implements http.CookieJar.SetCookies.
func (jar *ClearableCookieJar) SetCookies(u *url.URL, cookies []*http.Cookie) {
jar.jar.SetCookies(u, cookies)
}
func MacaroonsEqual(c *gc.C, ms1, ms2 []macaroon.Slice) error {
if len(ms1) != len(ms2) {
return errors.Errorf("length mismatch, %d vs %d", len(ms1), len(ms2))
}
for i := 0; i < len(ms1); i++ {
m1 := ms1[i]
m2 := ms2[i]
if len(m1) != len(m2) {
return errors.Errorf("length mismatch, %d vs %d", len(m1), len(m2))
}
for i := 0; i < len(m1); i++ {
MacaroonEquals(c, m1[i], m2[i])
}
}
return nil
}
func MacaroonEquals(c *gc.C, m1, m2 *macaroon.Macaroon) {
c.Assert(m1.Id(), jc.DeepEquals, m2.Id())
c.Assert(m1.Signature(), jc.DeepEquals, m2.Signature())
c.Assert(m1.Location(), gc.Equals, m2.Location())
}
func NewMacaroon(id string) (*macaroon.Macaroon, error) {
return macaroon.New(nil, []byte(id), "", macaroon.LatestVersion)
}
func MustNewMacaroon(id string) *macaroon.Macaroon {
mac, err := NewMacaroon(id)
if err != nil {
panic(err)
}
return mac
}