OLD | NEW |
1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
4 | 4 |
5 package prod | 5 package prod |
6 | 6 |
7 import ( | 7 import ( |
8 "fmt" | 8 "fmt" |
9 "net/http" | 9 "net/http" |
10 "net/http/cookiejar" | 10 "net/http/cookiejar" |
(...skipping 14 matching lines...) Expand all Loading... |
25 "https://www.googleapis.com/auth/cloud.platform", | 25 "https://www.googleapis.com/auth/cloud.platform", |
26 } | 26 } |
27 | 27 |
28 type key int | 28 type key int |
29 | 29 |
30 var ( | 30 var ( |
31 prodStateKey = "contains the current *prodState" | 31 prodStateKey = "contains the current *prodState" |
32 probeCacheKey = "contains the current *infoProbeCache" | 32 probeCacheKey = "contains the current *infoProbeCache" |
33 ) | 33 ) |
34 | 34 |
35 // AEContext retrieves the raw "google.golang.org/appengine" compatible Context. | 35 // getAEContext retrieves the raw "google.golang.org/appengine" compatible |
| 36 // Context. |
36 // | 37 // |
37 // It also transfers deadline of `c` to AE context, since deadline is used for | 38 // This is an independent Context chain from `c`. In an attempt to maintain user |
38 // RPCs. Doesn't transfer cancelation ability though (since it's ignored by GAE | 39 // expectations, the deadline of `c` is transferred to the returned Context, |
39 // anyway). | 40 // RPCs. Cancelation is not transferred. |
40 func AEContext(c context.Context) context.Context { | 41 func getAEContext(c context.Context) context.Context { |
41 ps := getProdState(c) | 42 ps := getProdState(c) |
42 return ps.context(c) | 43 return ps.context(c) |
43 } | 44 } |
44 | 45 |
45 func setupAECtx(c, aeCtx context.Context) context.Context { | 46 func setupAECtx(c, aeCtx context.Context) context.Context { |
46 c = withProdState(c, prodState{ | 47 c = withProdState(c, prodState{ |
47 ctx: aeCtx, | 48 ctx: aeCtx, |
48 noTxnCtx: aeCtx, | 49 noTxnCtx: aeCtx, |
49 }) | 50 }) |
50 return useModule(useMail(useUser(useURLFetch(useRDS(useMC(useTQ(useGI(us
eLogging(c))))))))) | 51 return useModule(useMail(useUser(useURLFetch(useRDS(useMC(useTQ(useGI(us
eLogging(c))))))))) |
51 } | 52 } |
52 | 53 |
53 // Use adds production implementations for all the gae services to the | 54 // Use adds production implementations for all the gae services to the |
54 // context. | 55 // context. The implementations are all backed by the real appengine SDK |
| 56 // functionality. |
55 // | 57 // |
56 // The services added are: | 58 // The services added are: |
57 // - github.com/luci-go/common/logging | 59 // - github.com/luci-go/common/logging |
58 // - github.com/luci/gae/service/datastore | 60 // - github.com/luci/gae/service/datastore |
59 // - github.com/luci/gae/service/info | 61 // - github.com/luci/gae/service/info |
60 // - github.com/luci/gae/service/mail | 62 // - github.com/luci/gae/service/mail |
61 // - github.com/luci/gae/service/memcache | 63 // - github.com/luci/gae/service/memcache |
62 // - github.com/luci/gae/service/module | 64 // - github.com/luci/gae/service/module |
63 // - github.com/luci/gae/service/taskqueue | 65 // - github.com/luci/gae/service/taskqueue |
64 // - github.com/luci/gae/service/urlfetch | 66 // - github.com/luci/gae/service/urlfetch |
65 // - github.com/luci/gae/service/user | 67 // - github.com/luci/gae/service/user |
66 // | 68 // |
67 // These can be retrieved with the <service>.Get functions. | 69 // These can be retrieved with the <service>.Get functions. |
68 // | 70 // |
69 // The implementations are all backed by the real appengine SDK functionality, | 71 // It is important to note that this DOES NOT install the AppEngine SDK into the |
| 72 // supplied Context. In general, using the raw AppEngine SDK to access a service |
| 73 // that is covered by luci/gae is dangerous, leading to a number of potential |
| 74 // pitfalls including inconsistent transaction management and data corruption. |
| 75 // |
| 76 // Users who wish to access the raw AppEngine SDK must derive their own |
| 77 // AppEngine Context at their own risk. |
70 func Use(c context.Context, r *http.Request) context.Context { | 78 func Use(c context.Context, r *http.Request) context.Context { |
71 return setupAECtx(c, appengine.NewContext(r)) | 79 return setupAECtx(c, appengine.NewContext(r)) |
72 } | 80 } |
73 | 81 |
74 // UseRemote is the same as Use, except that it lets you attach a context to | 82 // UseRemote is the same as Use, except that it lets you attach a context to |
75 // a remote host using the Remote API feature. See the docs for the | 83 // a remote host using the Remote API feature. See the docs for the |
76 // prerequisites. | 84 // prerequisites. |
77 // | 85 // |
78 // docs: https://cloud.google.com/appengine/docs/go/tools/remoteapi | 86 // docs: https://cloud.google.com/appengine/docs/go/tools/remoteapi |
79 // | 87 // |
80 // inOutCtx will be replaced with the new, derived context, if err is nil, | 88 // inOutCtx will be replaced with the new, derived context, if err is nil, |
81 // otherwise it's unchanged and continues to be safe-to-use. | 89 // otherwise it's unchanged and continues to be safe-to-use. |
82 // | 90 // |
83 // If client is nil, this will use create a new client, and will try to be | 91 // If client is nil, this will use create a new client, and will try to be |
84 // clever about it: | 92 // clever about it: |
85 // * If you're creating a remote context FROM AppEngine, this will use | 93 // * If you're creating a remote context FROM AppEngine, this will use |
86 // urlfetch.Transport. This can be used to allow app-to-app remote_api | 94 // urlfetch.Transport. This can be used to allow app-to-app remote_api |
87 // control. | 95 // control. |
88 // | 96 // |
89 // * If host starts with "localhost", this will create a regular http.Client | 97 // * If host starts with "localhost", this will create a regular http.Client |
90 // with a cookiejar, and call the _ah/login API to log in as an admin with | 98 // with a cookiejar, and call the _ah/login API to log in as an admin with |
91 // the user "admin@example.com". | 99 // the user "admin@example.com". |
92 // | 100 // |
93 // * Otherwise, it will create a Google OAuth2 client with the following scope
s: | 101 // * Otherwise, it will create a Google OAuth2 client with the following scope
s: |
94 // - "https://www.googleapis.com/auth/appengine.apis" | 102 // - "https://www.googleapis.com/auth/appengine.apis" |
95 // - "https://www.googleapis.com/auth/userinfo.email" | 103 // - "https://www.googleapis.com/auth/userinfo.email" |
96 // - "https://www.googleapis.com/auth/cloud.platform" | 104 // - "https://www.googleapis.com/auth/cloud.platform" |
| 105 // |
| 106 // It is important to note that this DOES NOT install the AppEngine SDK into the |
| 107 // supplied Context. See the warning in Use for more information. |
97 func UseRemote(inOutCtx *context.Context, host string, client *http.Client) (err
error) { | 108 func UseRemote(inOutCtx *context.Context, host string, client *http.Client) (err
error) { |
98 if client == nil { | 109 if client == nil { |
99 » » aeCtx := AEContext(*inOutCtx) | 110 » » aeCtx := getAEContext(*inOutCtx) |
100 | 111 |
101 if strings.HasPrefix(host, "localhost") { | 112 if strings.HasPrefix(host, "localhost") { |
102 transp := http.DefaultTransport | 113 transp := http.DefaultTransport |
103 if aeCtx != nil { | 114 if aeCtx != nil { |
104 transp = urlfetch.Get(*inOutCtx) | 115 transp = urlfetch.Get(*inOutCtx) |
105 } | 116 } |
106 | 117 |
107 client = &http.Client{Transport: transp} | 118 client = &http.Client{Transport: transp} |
108 client.Jar, err = cookiejar.New(nil) | 119 client.Jar, err = cookiejar.New(nil) |
109 if err != nil { | 120 if err != nil { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 aeCtx := ps.ctx | 188 aeCtx := ps.ctx |
178 if aeCtx == nil { | 189 if aeCtx == nil { |
179 return nil | 190 return nil |
180 } | 191 } |
181 | 192 |
182 if deadline, ok := c.Deadline(); ok { | 193 if deadline, ok := c.Deadline(); ok { |
183 aeCtx, _ = context.WithDeadline(aeCtx, deadline) | 194 aeCtx, _ = context.WithDeadline(aeCtx, deadline) |
184 } | 195 } |
185 return aeCtx | 196 return aeCtx |
186 } | 197 } |
OLD | NEW |