forked from lizan/service-control-client-cxx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsignature.cc
96 lines (74 loc) · 3.08 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
/* Copyright 2016 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 "utils/md5.h"
using std::string;
using google::api::servicecontrol::v1::CheckRequest;
using google::api::servicecontrol::v1::MetricValue;
using google::api::servicecontrol::v1::Operation;
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);
for (const auto& metric_value_set : operation.metric_value_sets()) {
hasher.Update(kDelimiter, kDelimiterLength);
hasher.Update(metric_value_set.metric_name());
for (const auto& metric_value : metric_value_set.metric_values()) {
UpdateHashMetricValue(metric_value, &hasher);
}
}
hasher.Update(kDelimiter, kDelimiterLength);
return hasher.Digest();
}
} // namespace service_control_client
} // namespace google