This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
forked from lizan/service-control-client-cxx
-
Notifications
You must be signed in to change notification settings - Fork 10
/
signature.cc
133 lines (104 loc) · 4.26 KB
/
signature.cc
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
/* Copyright 2017 Google Inc. All Rights Reserved.
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 "src/signature.h"
#include <map>
#include <set>
#include "utils/md5.h"
using google::api::servicecontrol::v1::AllocateQuotaRequest;
using google::api::servicecontrol::v1::CheckRequest;
using google::api::servicecontrol::v1::MetricValue;
using google::api::servicecontrol::v1::Operation;
using google::api::servicecontrol::v1::QuotaOperation;
using std::string;
namespace google {
namespace service_control_client {
namespace {
const char kDelimiter[] = "\0";
const int kDelimiterLength = 1;
// Updates the give hasher with the given labels.
void UpdateHashLabels(const ::google::protobuf::Map<string, string>& labels,
MD5* hasher) {
std::map<string, string> ordered_labels(labels.begin(), labels.end());
for (const auto& label : ordered_labels) {
// Note we must use the Update(void const *data, int size) function here
// for the delimiter instead of Update(StringPiece data), because
// StringPiece would use strlen and gets zero length.
hasher->Update(kDelimiter, kDelimiterLength);
hasher->Update(label.first);
hasher->Update(kDelimiter, kDelimiterLength);
hasher->Update(label.second);
}
}
// Updates the give hasher with the given metric value.
void UpdateHashMetricValue(const MetricValue& metric_value, MD5* hasher) {
UpdateHashLabels(metric_value.labels(), hasher);
}
} // namespace
string GenerateReportOperationSignature(const Operation& operation) {
MD5 hasher;
hasher.Update(operation.consumer_id());
hasher.Update(kDelimiter, kDelimiterLength);
hasher.Update(operation.operation_name());
UpdateHashLabels(operation.labels(), &hasher);
return hasher.Digest();
}
string GenerateReportMetricValueSignature(const MetricValue& metric_value) {
MD5 hasher;
UpdateHashMetricValue(metric_value, &hasher);
return hasher.Digest();
}
string GenerateCheckRequestSignature(const CheckRequest& request) {
MD5 hasher;
const Operation& operation = request.operation();
hasher.Update(operation.operation_name());
hasher.Update(kDelimiter, kDelimiterLength);
hasher.Update(operation.consumer_id());
hasher.Update(kDelimiter, kDelimiterLength);
UpdateHashLabels(operation.labels(), &hasher);
// keep sorted order of metric_name
std::map<std::string, const ::google::protobuf::RepeatedPtrField<
::google::api::servicecontrol::v1::MetricValue>*>
metric_value_set_map;
for (const auto& metric_value_set : operation.metric_value_sets()) {
metric_value_set_map[metric_value_set.metric_name()] =
&metric_value_set.metric_values();
}
for (const auto& metric_value_set : metric_value_set_map) {
hasher.Update(kDelimiter, kDelimiterLength);
hasher.Update(metric_value_set.first);
for (const auto& metric_value : *metric_value_set.second) {
UpdateHashMetricValue(metric_value, &hasher);
}
}
hasher.Update(kDelimiter, kDelimiterLength);
return hasher.Digest();
}
string GenerateAllocateQuotaRequestSignature(
const AllocateQuotaRequest& request) {
MD5 hasher;
const QuotaOperation& operation = request.allocate_operation();
hasher.Update(operation.method_name());
hasher.Update(kDelimiter, kDelimiterLength);
hasher.Update(operation.consumer_id());
// order of metric_name can be changed. need to be sorted.
std::set<std::string> metric_names;
for (const auto& metric_value_set : operation.quota_metrics()) {
metric_names.insert(metric_value_set.metric_name());
}
for (const auto& metric_name : metric_names) {
hasher.Update(kDelimiter, kDelimiterLength);
hasher.Update(metric_name);
}
return hasher.Digest();
}
} // namespace service_control_client
} // namespace google