Hazelcast C++ Client
Hazelcast C++ Client Library
ec2_request_signer.h
1 /*
2  * Copyright (c) 2008-2021, Hazelcast, Inc. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <string>
19 #include <unordered_map>
20 #include <vector>
21 
22 #include "hazelcast/util/export.h"
23 
24 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
25 #pragma warning(push)
26 #pragma warning(disable: 4251) //for dll export
27 #endif
28 
29 namespace hazelcast {
30  namespace client {
31  namespace config {
32  class client_aws_config;
33  }
34  namespace aws {
35  namespace security {
36  class HAZELCAST_API ec2_request_signer {
37  public:
38  ec2_request_signer(const config::client_aws_config &aws_config, const std::string &timestamp,
39  const std::string &endpoint);
40 
41  virtual ~ec2_request_signer();
42 
43  std::string sign(const std::unordered_map<std::string, std::string> &attributes);
44 
45  std::string create_formatted_credential() const;
46 
47  std::string get_canonicalized_query_string(const std::unordered_map<std::string, std::string> &attributes) const;
48  private:
49  /* Task 1 */
50  std::string get_canonicalized_request(const std::unordered_map<std::string, std::string> &attributes) const;
51 
52  std::string get_canonical_headers() const;
53 
54  std::string get_canonicalized_query_string(const std::vector<std::string> &list) const;
55 
56  std::vector<std::string> get_list_of_entries(const std::unordered_map<std::string, std::string> &entries) const;
57 
58  static std::string format_attribute(const std::string &key, const std::string &value);
59 
60  /* Task 2 */
61  std::string create_string_to_sign(const std::string &canonical_request) const;
62 
63  std::string get_credential_scope() const;
64 
65  /* Task 3 */
66  std::vector<unsigned char> derive_signing_key() const;
67 
68  std::string create_signature(const std::string &string_to_sign, const std::vector<unsigned char> &signing_key) const;
69 
70  std::string hmac_sh_a256_hex(const std::vector<unsigned char> &key, const std::string &msg) const;
71 
72  std::string convert_to_hex_string(const unsigned char *buffer, unsigned int len) const;
73 
74  unsigned int hmac_sh_a256_bytes(const void *key, int key_len, const std::string &msg,
75  unsigned char *hash) const;
76 
77  unsigned int hmac_sh_a256_bytes(const std::string &key, const std::string &msg,
78  unsigned char *hash) const;
79 
80  unsigned int hmac_sh_a256_bytes(const std::vector<unsigned char> &key, const std::string &msg,
81  unsigned char *hash) const;
82 
83  unsigned int hmac_sh_a256_bytes(const void *key_buffer, int key_len, const unsigned char *data,
84  size_t data_len,
85  unsigned char *hash) const;
86 
87  std::string sha256_hashhex(const std::string &in) const;
88 
89  static std::string NEW_LINE;
90  static size_t DATE_LENGTH;
91 
92  const config::client_aws_config &aws_config_;
93  std::string timestamp_;
94  const std::string &endpoint_;
95  };
96  }
97  }
98  }
99 }
100 
101 #if defined(WIN32) || defined(_WIN32) || defined(WIN64) || defined(_WIN64)
102 #pragma warning(pop)
103 #endif
104 
105 
The client_aws_config contains the configuration for client to connect to nodes in aws environment.
Endpoint represents a peer in the cluster.
Definition: endpoint.h:34