summaryrefslogtreecommitdiff
path: root/grpc/src/core/lib/security/authorization/evaluate_args.cc
blob: 6eaf10670da600045c176b43a09a0e5409a296ec (plain)
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
//
//
// Copyright 2020 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//

#include <grpc/support/port_platform.h>

#include "src/core/lib/security/authorization/evaluate_args.h"

#include "src/core/lib/iomgr/parse_address.h"
#include "src/core/lib/iomgr/resolve_address.h"
#include "src/core/lib/iomgr/sockaddr_utils.h"
#include "src/core/lib/slice/slice_utils.h"

namespace grpc_core {

absl::string_view EvaluateArgs::GetPath() const {
  absl::string_view path;
  if (metadata_ != nullptr && metadata_->idx.named.path != nullptr) {
    grpc_linked_mdelem* elem = metadata_->idx.named.path;
    const grpc_slice& val = GRPC_MDVALUE(elem->md);
    path = StringViewFromSlice(val);
  }
  return path;
}

absl::string_view EvaluateArgs::GetHost() const {
  absl::string_view host;
  if (metadata_ != nullptr && metadata_->idx.named.host != nullptr) {
    grpc_linked_mdelem* elem = metadata_->idx.named.host;
    const grpc_slice& val = GRPC_MDVALUE(elem->md);
    host = StringViewFromSlice(val);
  }
  return host;
}

absl::string_view EvaluateArgs::GetMethod() const {
  absl::string_view method;
  if (metadata_ != nullptr && metadata_->idx.named.method != nullptr) {
    grpc_linked_mdelem* elem = metadata_->idx.named.method;
    const grpc_slice& val = GRPC_MDVALUE(elem->md);
    method = StringViewFromSlice(val);
  }
  return method;
}

std::multimap<absl::string_view, absl::string_view> EvaluateArgs::GetHeaders()
    const {
  std::multimap<absl::string_view, absl::string_view> headers;
  if (metadata_ == nullptr) {
    return headers;
  }
  for (grpc_linked_mdelem* elem = metadata_->list.head; elem != nullptr;
       elem = elem->next) {
    const grpc_slice& key = GRPC_MDKEY(elem->md);
    const grpc_slice& val = GRPC_MDVALUE(elem->md);
    headers.emplace(StringViewFromSlice(key), StringViewFromSlice(val));
  }
  return headers;
}

absl::string_view EvaluateArgs::GetLocalAddress() const {
  absl::string_view addr = grpc_endpoint_get_local_address(endpoint_);
  size_t first_colon = addr.find(":");
  size_t last_colon = addr.rfind(":");
  if (first_colon == std::string::npos || last_colon == std::string::npos) {
    return "";
  } else {
    return addr.substr(first_colon + 1, last_colon - first_colon - 1);
  }
}

int EvaluateArgs::GetLocalPort() const {
  if (endpoint_ == nullptr) {
    return 0;
  }
  absl::StatusOr<URI> uri =
      URI::Parse(grpc_endpoint_get_local_address(endpoint_));
  grpc_resolved_address resolved_addr;
  if (!uri.ok() || !grpc_parse_uri(*uri, &resolved_addr)) {
    return 0;
  }
  return grpc_sockaddr_get_port(&resolved_addr);
}

absl::string_view EvaluateArgs::GetPeerAddress() const {
  absl::string_view addr = grpc_endpoint_get_peer(endpoint_);
  size_t first_colon = addr.find(":");
  size_t last_colon = addr.rfind(":");
  if (first_colon == std::string::npos || last_colon == std::string::npos) {
    return "";
  } else {
    return addr.substr(first_colon + 1, last_colon - first_colon - 1);
  }
}

int EvaluateArgs::GetPeerPort() const {
  if (endpoint_ == nullptr) {
    return 0;
  }
  absl::StatusOr<URI> uri = URI::Parse(grpc_endpoint_get_peer(endpoint_));
  grpc_resolved_address resolved_addr;
  if (!uri.ok() || !grpc_parse_uri(*uri, &resolved_addr)) {
    return 0;
  }
  return grpc_sockaddr_get_port(&resolved_addr);
}

absl::string_view EvaluateArgs::GetSpiffeId() const {
  if (auth_context_ == nullptr) {
    return "";
  }
  grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name(
      auth_context_, GRPC_PEER_SPIFFE_ID_PROPERTY_NAME);
  const grpc_auth_property* prop = grpc_auth_property_iterator_next(&it);
  if (prop == nullptr || grpc_auth_property_iterator_next(&it) != nullptr) {
    return "";
  }
  return absl::string_view(prop->value, prop->value_length);
}

absl::string_view EvaluateArgs::GetCertServerName() const {
  if (auth_context_ == nullptr) {
    return "";
  }
  grpc_auth_property_iterator it = grpc_auth_context_find_properties_by_name(
      auth_context_, GRPC_X509_CN_PROPERTY_NAME);
  const grpc_auth_property* prop = grpc_auth_property_iterator_next(&it);
  if (prop == nullptr || grpc_auth_property_iterator_next(&it) != nullptr) {
    return "";
  }
  return absl::string_view(prop->value, prop->value_length);
}

}  // namespace grpc_core