| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 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 machine implements authentication based on LUCI machine tokens. | 5 // Package machine implements authentication based on LUCI machine tokens. |
| 6 package machine | 6 package machine |
| 7 | 7 |
| 8 import ( | 8 import ( |
| 9 "encoding/base64" | 9 "encoding/base64" |
| 10 "fmt" | 10 "fmt" |
| 11 "net/http" | 11 "net/http" |
| 12 "time" | 12 "time" |
| 13 | 13 |
| 14 "github.com/golang/protobuf/proto" | 14 "github.com/golang/protobuf/proto" |
| 15 "golang.org/x/net/context" | 15 "golang.org/x/net/context" |
| 16 | 16 |
| 17 "github.com/luci/luci-go/common/clock" | 17 "github.com/luci/luci-go/common/clock" |
| 18 "github.com/luci/luci-go/common/errors" | 18 "github.com/luci/luci-go/common/errors" |
| 19 "github.com/luci/luci-go/common/logging" | 19 "github.com/luci/luci-go/common/logging" |
| 20 "github.com/luci/luci-go/common/retry" |
| 20 "github.com/luci/luci-go/server/auth" | 21 "github.com/luci/luci-go/server/auth" |
| 21 "github.com/luci/luci-go/server/auth/identity" | 22 "github.com/luci/luci-go/server/auth/identity" |
| 22 "github.com/luci/luci-go/server/auth/signing" | 23 "github.com/luci/luci-go/server/auth/signing" |
| 23 | 24 |
| 24 "github.com/luci/luci-go/tokenserver/api" | 25 "github.com/luci/luci-go/tokenserver/api" |
| 25 ) | 26 ) |
| 26 | 27 |
| 27 const ( | 28 const ( |
| 28 // MachineTokenHeader is an HTTP header that carries the machine token. | 29 // MachineTokenHeader is an HTTP header that carries the machine token. |
| 29 MachineTokenHeader = "X-Luci-Machine-Token" | 30 MachineTokenHeader = "X-Luci-Machine-Token" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 // it belongs to "auth-token-servers" group. | 84 // it belongs to "auth-token-servers" group. |
| 84 signerServiceAccount, err := identity.MakeIdentity("user:" + body.Issued
By) | 85 signerServiceAccount, err := identity.MakeIdentity("user:" + body.Issued
By) |
| 85 if err != nil { | 86 if err != nil { |
| 86 logTokenError(c, r, body, err, "Bad issued_by field - %q", body.
IssuedBy) | 87 logTokenError(c, r, body, err, "Bad issued_by field - %q", body.
IssuedBy) |
| 87 return nil, ErrBadToken | 88 return nil, ErrBadToken |
| 88 } | 89 } |
| 89 | 90 |
| 90 // Reject tokens from unknown token servers right away. | 91 // Reject tokens from unknown token servers right away. |
| 91 db, err := auth.GetDB(c) | 92 db, err := auth.GetDB(c) |
| 92 if err != nil { | 93 if err != nil { |
| 93 » » return nil, errors.WrapTransient(err) | 94 » » return nil, retry.Tag.Apply(err) |
| 94 } | 95 } |
| 95 ok, err := db.IsMember(c, signerServiceAccount, TokenServersGroup) | 96 ok, err := db.IsMember(c, signerServiceAccount, TokenServersGroup) |
| 96 if err != nil { | 97 if err != nil { |
| 97 » » return nil, errors.WrapTransient(err) | 98 » » return nil, retry.Tag.Apply(err) |
| 98 } | 99 } |
| 99 if !ok { | 100 if !ok { |
| 100 logTokenError(c, r, body, nil, "Unknown token issuer - %q", body
.IssuedBy) | 101 logTokenError(c, r, body, nil, "Unknown token issuer - %q", body
.IssuedBy) |
| 101 return nil, ErrBadToken | 102 return nil, ErrBadToken |
| 102 } | 103 } |
| 103 | 104 |
| 104 // Check the expiration time before doing any heavier checks. | 105 // Check the expiration time before doing any heavier checks. |
| 105 if err = checkExpiration(body, clock.Now(c)); err != nil { | 106 if err = checkExpiration(body, clock.Now(c)); err != nil { |
| 106 logTokenError(c, r, body, err, "Token has expired or not yet val
id") | 107 logTokenError(c, r, body, err, "Token has expired or not yet val
id") |
| 107 return nil, ErrBadToken | 108 return nil, ErrBadToken |
| 108 } | 109 } |
| 109 | 110 |
| 110 // Check the token was actually signed by the server. | 111 // Check the token was actually signed by the server. |
| 111 if err = m.checkSignature(c, body.IssuedBy, envelope); err != nil { | 112 if err = m.checkSignature(c, body.IssuedBy, envelope); err != nil { |
| 112 » » if errors.IsTransient(err) { | 113 » » if retry.Tag.In(err) { |
| 113 return nil, err | 114 return nil, err |
| 114 } | 115 } |
| 115 logTokenError(c, r, body, err, "Bad signature") | 116 logTokenError(c, r, body, err, "Bad signature") |
| 116 return nil, ErrBadToken | 117 return nil, ErrBadToken |
| 117 } | 118 } |
| 118 | 119 |
| 119 // The token is valid. Construct the bot identity. | 120 // The token is valid. Construct the bot identity. |
| 120 botIdent, err := identity.MakeIdentity("bot:" + body.MachineFqdn) | 121 botIdent, err := identity.MakeIdentity("bot:" + body.MachineFqdn) |
| 121 if err != nil { | 122 if err != nil { |
| 122 logTokenError(c, r, body, err, "Bad machine_fqdn - %q", body.Mac
hineFqdn) | 123 logTokenError(c, r, body, err, "Bad machine_fqdn - %q", body.Mac
hineFqdn) |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 180 | 181 |
| 181 // checkSignature verifies the token signature. | 182 // checkSignature verifies the token signature. |
| 182 func (m *MachineTokenAuthMethod) checkSignature(c context.Context, signerEmail s
tring, envelope *tokenserver.MachineTokenEnvelope) error { | 183 func (m *MachineTokenAuthMethod) checkSignature(c context.Context, signerEmail s
tring, envelope *tokenserver.MachineTokenEnvelope) error { |
| 183 // Note that FetchCertificatesForServiceAccount implements caching insid
e. | 184 // Note that FetchCertificatesForServiceAccount implements caching insid
e. |
| 184 fetcher := m.certsFetcher | 185 fetcher := m.certsFetcher |
| 185 if fetcher == nil { | 186 if fetcher == nil { |
| 186 fetcher = signing.FetchCertificatesForServiceAccount | 187 fetcher = signing.FetchCertificatesForServiceAccount |
| 187 } | 188 } |
| 188 certs, err := fetcher(c, signerEmail) | 189 certs, err := fetcher(c, signerEmail) |
| 189 if err != nil { | 190 if err != nil { |
| 190 » » return errors.WrapTransient(err) | 191 » » return retry.Tag.Apply(err) |
| 191 } | 192 } |
| 192 return certs.CheckSignature(envelope.KeyId, envelope.TokenBody, envelope
.RsaSha256) | 193 return certs.CheckSignature(envelope.KeyId, envelope.TokenBody, envelope
.RsaSha256) |
| 193 } | 194 } |
| OLD | NEW |