/
export_test.go
192 lines (162 loc) · 5.81 KB
/
export_test.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
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"fmt"
"reflect"
"time"
"github.com/juju/names"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/macaroon-bakery.v1/bakery"
"gopkg.in/macaroon.v1"
"github.com/juju/juju/apiserver/authentication"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/rpc"
"github.com/juju/juju/state"
)
var (
RootType = reflect.TypeOf(&apiHandler{})
NewPingTimeout = newPingTimeout
MaxClientPingInterval = &maxClientPingInterval
MongoPingInterval = &mongoPingInterval
NewBackups = &newBackups
NewLogTailer = &newLogTailer
)
func ServerMacaroon(srv *Server) (*macaroon.Macaroon, error) {
auth, err := srv.authCtxt.macaroonAuth()
if err != nil {
return nil, err
}
return auth.(*authentication.MacaroonAuthenticator).Macaroon, nil
}
func ServerBakeryService(srv *Server) (*bakery.Service, error) {
auth, err := srv.authCtxt.macaroonAuth()
if err != nil {
return nil, err
}
return auth.(*authentication.MacaroonAuthenticator).Service, nil
}
// ServerAuthenticatorForTag calls the authenticatorForTag method
// of the server's authContext.
func ServerAuthenticatorForTag(srv *Server, tag names.Tag) (authentication.EntityAuthenticator, error) {
return srv.authCtxt.authenticatorForTag(tag)
}
func ApiHandlerWithEntity(entity state.Entity) *apiHandler {
return &apiHandler{entity: entity}
}
func ServerAuthenticator(srv *Server, tag names.Tag) authentication.EntityAuthenticator {
return srv.authCtxt
}
const LoginRateLimit = loginRateLimit
// DelayLogins changes how the Login code works so that logins won't proceed
// until they get a message on the returned channel.
// After calling this function, the caller is responsible for sending messages
// on the nextChan in order for Logins to succeed. The original behavior can be
// restored by calling the cleanup function.
func DelayLogins() (nextChan chan struct{}, cleanup func()) {
nextChan = make(chan struct{}, 10)
cleanup = func() {
doCheckCreds = checkCreds
}
delayedCheckCreds := func(st *state.State, c params.LoginRequest, lookForEnvUser bool, authenticator authentication.EntityAuthenticator) (state.Entity, *time.Time, error) {
<-nextChan
return checkCreds(st, c, lookForEnvUser, authenticator)
}
doCheckCreds = delayedCheckCreds
return
}
func NewErrRoot(err error) *errRoot {
return &errRoot{err}
}
// TestingApiRoot gives you an ApiRoot as a rpc.Methodfinder that is
// *barely* connected to anything. Just enough to let you probe some
// of the interfaces, but not enough to actually do any RPC calls.
func TestingApiRoot(st *state.State) rpc.MethodFinder {
return newApiRoot(st, common.NewResources(), nil)
}
// TestingApiHandler gives you an ApiHandler that isn't connected to
// anything real. It's enough to let test some basic functionality though.
func TestingApiHandler(c *gc.C, srvSt, st *state.State) (*apiHandler, *common.Resources) {
srv := &Server{
state: srvSt,
tag: names.NewMachineTag("0"),
}
h, err := newApiHandler(srv, st, nil, nil, st.EnvironUUID())
c.Assert(err, jc.ErrorIsNil)
return h, h.getResources()
}
// TestingUpgradingApiHandler returns a limited srvRoot
// in an upgrade scenario.
func TestingUpgradingRoot(st *state.State) rpc.MethodFinder {
r := TestingApiRoot(st)
return newUpgradingRoot(r)
}
// TestingRestrictedApiHandler returns a restricted srvRoot as if accessed
// from the root of the API path with a recent (verison > 1) login.
func TestingRestrictedApiHandler(st *state.State) rpc.MethodFinder {
r := TestingApiRoot(st)
return newRestrictedRoot(r)
}
type preFacadeAdminApi struct{}
func newPreFacadeAdminApi(srv *Server, root *apiHandler, reqNotifier *requestNotifier) interface{} {
return &preFacadeAdminApi{}
}
func (r *preFacadeAdminApi) Admin(id string) (*preFacadeAdminApi, error) {
return r, nil
}
var PreFacadeEnvironTag = names.NewEnvironTag("383c49f3-526d-4f9e-b50a-1e6fa4e9b3d9")
func (r *preFacadeAdminApi) Login(c params.Creds) (params.LoginResult, error) {
return params.LoginResult{
EnvironTag: PreFacadeEnvironTag.String(),
}, nil
}
type failAdminApi struct{}
func newFailAdminApi(srv *Server, root *apiHandler, reqNotifier *requestNotifier) interface{} {
return &failAdminApi{}
}
func (r *failAdminApi) Admin(id string) (*failAdminApi, error) {
return r, nil
}
func (r *failAdminApi) Login(c params.Creds) (params.LoginResult, error) {
return params.LoginResult{}, fmt.Errorf("fail")
}
// SetPreFacadeAdminApi is used to create a test scenario where the API server
// does not know about API facade versioning. In this case, the client should
// login to the v1 facade, which sends backwards-compatible login fields.
// The v0 facade will fail on a pre-defined error.
func SetPreFacadeAdminApi(srv *Server) {
srv.adminApiFactories = map[int]adminApiFactory{
0: newFailAdminApi,
1: newPreFacadeAdminApi,
}
}
func SetAdminApiVersions(srv *Server, versions ...int) {
factories := make(map[int]adminApiFactory)
for _, n := range versions {
switch n {
case 0:
factories[n] = newAdminApiV0
case 1:
factories[n] = newAdminApiV1
case 2:
factories[n] = newAdminApiV2
default:
panic(fmt.Errorf("unknown admin API version %d", n))
}
}
srv.adminApiFactories = factories
}
// TestingRestoreInProgressRoot returns a limited restoreInProgressRoot
// containing a srvRoot as returned by TestingSrvRoot.
func TestingRestoreInProgressRoot(st *state.State) *restoreInProgressRoot {
r := TestingApiRoot(st)
return newRestoreInProgressRoot(r)
}
// TestingAboutToRestoreRoot returns a limited aboutToRestoreRoot
// containing a srvRoot as returned by TestingSrvRoot.
func TestingAboutToRestoreRoot(st *state.State) *aboutToRestoreRoot {
r := TestingApiRoot(st)
return newAboutToRestoreRoot(r)
}