authinfo.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. // Package authinfo provide authentication information returned by handshakers.
  19. package authinfo
  20. import (
  21. "google.golang.org/grpc/credentials"
  22. altspb "google.golang.org/grpc/credentials/alts/internal/proto/grpc_gcp"
  23. )
  24. var _ credentials.AuthInfo = (*altsAuthInfo)(nil)
  25. // altsAuthInfo exposes security information from the ALTS handshake to the
  26. // application. altsAuthInfo is immutable and implements credentials.AuthInfo.
  27. type altsAuthInfo struct {
  28. p *altspb.AltsContext
  29. credentials.CommonAuthInfo
  30. }
  31. // New returns a new altsAuthInfo object given handshaker results.
  32. func New(result *altspb.HandshakerResult) credentials.AuthInfo {
  33. return newAuthInfo(result)
  34. }
  35. func newAuthInfo(result *altspb.HandshakerResult) *altsAuthInfo {
  36. return &altsAuthInfo{
  37. p: &altspb.AltsContext{
  38. ApplicationProtocol: result.GetApplicationProtocol(),
  39. RecordProtocol: result.GetRecordProtocol(),
  40. // TODO: assign security level from result.
  41. SecurityLevel: altspb.SecurityLevel_INTEGRITY_AND_PRIVACY,
  42. PeerServiceAccount: result.GetPeerIdentity().GetServiceAccount(),
  43. LocalServiceAccount: result.GetLocalIdentity().GetServiceAccount(),
  44. PeerRpcVersions: result.GetPeerRpcVersions(),
  45. PeerAttributes: result.GetPeerIdentity().GetAttributes(),
  46. },
  47. CommonAuthInfo: credentials.CommonAuthInfo{SecurityLevel: credentials.PrivacyAndIntegrity},
  48. }
  49. }
  50. // AuthType identifies the context as providing ALTS authentication information.
  51. func (s *altsAuthInfo) AuthType() string {
  52. return "alts"
  53. }
  54. // ApplicationProtocol returns the context's application protocol.
  55. func (s *altsAuthInfo) ApplicationProtocol() string {
  56. return s.p.GetApplicationProtocol()
  57. }
  58. // RecordProtocol returns the context's record protocol.
  59. func (s *altsAuthInfo) RecordProtocol() string {
  60. return s.p.GetRecordProtocol()
  61. }
  62. // SecurityLevel returns the context's security level.
  63. func (s *altsAuthInfo) SecurityLevel() altspb.SecurityLevel {
  64. return s.p.GetSecurityLevel()
  65. }
  66. // PeerServiceAccount returns the context's peer service account.
  67. func (s *altsAuthInfo) PeerServiceAccount() string {
  68. return s.p.GetPeerServiceAccount()
  69. }
  70. // LocalServiceAccount returns the context's local service account.
  71. func (s *altsAuthInfo) LocalServiceAccount() string {
  72. return s.p.GetLocalServiceAccount()
  73. }
  74. // PeerRPCVersions returns the context's peer RPC versions.
  75. func (s *altsAuthInfo) PeerRPCVersions() *altspb.RpcProtocolVersions {
  76. return s.p.GetPeerRpcVersions()
  77. }
  78. // PeerAttributes returns the context's peer attributes.
  79. func (s *altsAuthInfo) PeerAttributes() map[string]string {
  80. return s.p.GetPeerAttributes()
  81. }