| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2017 The LUCI Authors. |
| 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at |
| 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. |
| 14 |
| 15 package serviceaccounts |
| 16 |
| 17 import ( |
| 18 "testing" |
| 19 "time" |
| 20 |
| 21 "golang.org/x/net/context" |
| 22 |
| 23 "github.com/luci/gae/service/info" |
| 24 "github.com/luci/luci-go/appengine/gaetesting" |
| 25 "github.com/luci/luci-go/common/clock/testclock" |
| 26 "github.com/luci/luci-go/common/config/impl/memory" |
| 27 "github.com/luci/luci-go/luci_config/server/cfgclient/backend/testconfig
" |
| 28 admin "github.com/luci/luci-go/tokenserver/api/admin/v1" |
| 29 "github.com/luci/luci-go/tokenserver/appengine/impl/utils/policy" |
| 30 |
| 31 . "github.com/luci/luci-go/common/testing/assertions" |
| 32 . "github.com/smartystreets/goconvey/convey" |
| 33 ) |
| 34 |
| 35 func TestImportServiceAccountsConfigs(t *testing.T) { |
| 36 t.Parallel() |
| 37 |
| 38 Convey("Works", t, func() { |
| 39 ctx := gaetesting.TestingContext() |
| 40 ctx, clk := testclock.UseTime(ctx, testclock.TestTimeUTC) |
| 41 |
| 42 ctx = prepareCfg(ctx, `rules { |
| 43 name: "rule 1" |
| 44 owner: "developer@example.com" |
| 45 service_account: "abc@robots.com" |
| 46 allowed_scope: "https://scope" |
| 47 end_user: "user:abc@example.com" |
| 48 end_user: "group:group-name" |
| 49 proxy: "user:proxy@example.com" |
| 50 max_grant_validity_duration: 3600 |
| 51 }`) |
| 52 |
| 53 rules := NewRulesCache() |
| 54 rpc := ImportServiceAccountsConfigsRPC{RulesCache: rules} |
| 55 |
| 56 // No config. |
| 57 r, err := rules.Rules(ctx) |
| 58 So(err, ShouldEqual, policy.ErrNoPolicy) |
| 59 |
| 60 resp, err := rpc.ImportServiceAccountsConfigs(ctx, nil) |
| 61 So(err, ShouldBeNil) |
| 62 So(resp, ShouldResemble, &admin.ImportedConfigs{ |
| 63 Revision: "16d15198a351a6a9beb7afe6f3485c9a47b18f7d", |
| 64 }) |
| 65 |
| 66 // Have config now. |
| 67 r, err = rules.Rules(ctx) |
| 68 So(err, ShouldBeNil) |
| 69 So(r.ConfigRevision(), ShouldEqual, "16d15198a351a6a9beb7afe6f34
85c9a47b18f7d") |
| 70 |
| 71 // Noop import. |
| 72 resp, err = rpc.ImportServiceAccountsConfigs(ctx, nil) |
| 73 So(err, ShouldBeNil) |
| 74 So(resp.Revision, ShouldEqual, "16d15198a351a6a9beb7afe6f3485c9a
47b18f7d") |
| 75 |
| 76 // Try to import completely broken config. |
| 77 ctx = prepareCfg(ctx, `I'm broken`) |
| 78 _, err = rpc.ImportServiceAccountsConfigs(ctx, nil) |
| 79 So(err, ShouldErrLike, `line 1.0: unknown field name`) |
| 80 |
| 81 // Old config is not replaced. |
| 82 r, _ = rules.Rules(ctx) |
| 83 So(r.ConfigRevision(), ShouldEqual, "16d15198a351a6a9beb7afe6f34
85c9a47b18f7d") |
| 84 |
| 85 // Roll time to expire local rules cache. |
| 86 clk.Add(10 * time.Minute) |
| 87 |
| 88 // Have new config now! |
| 89 ctx = prepareCfg(ctx, `rules { |
| 90 name: "rule 2" |
| 91 owner: "developer@example.com" |
| 92 service_account: "abc@robots.com" |
| 93 allowed_scope: "https://scope" |
| 94 end_user: "user:abc@example.com" |
| 95 end_user: "group:group-name" |
| 96 proxy: "user:proxy@example.com" |
| 97 max_grant_validity_duration: 3600 |
| 98 }`) |
| 99 |
| 100 // Import it. |
| 101 resp, err = rpc.ImportServiceAccountsConfigs(ctx, nil) |
| 102 So(err, ShouldBeNil) |
| 103 So(resp, ShouldResemble, &admin.ImportedConfigs{ |
| 104 Revision: "663ea6e319c41b6fcee65d8d1d5c758813aa0fb1", |
| 105 }) |
| 106 |
| 107 // It is now active. |
| 108 r, err = rules.Rules(ctx) |
| 109 So(err, ShouldBeNil) |
| 110 So(r.ConfigRevision(), ShouldEqual, "663ea6e319c41b6fcee65d8d1d5
c758813aa0fb1") |
| 111 }) |
| 112 } |
| 113 |
| 114 func prepareCfg(c context.Context, configFile string) context.Context { |
| 115 return testconfig.WithCommonClient(c, memory.New(map[string]memory.Confi
gSet{ |
| 116 "services/" + info.AppID(c): { |
| 117 "service_accounts.cfg": configFile, |
| 118 }, |
| 119 })) |
| 120 } |
| OLD | NEW |