AOSharedServiceLibrary
consul_admin.h
1 /*
2 MIT License Block
3 
4 Copyright (c) 2016 Alex Barry
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24 
25 #include <string.h>
26 #include <string>
27 #include <vector>
28 #include <iostream>
29 
30 #include "aossl/core/include/base_http_client.h"
31 #include "aossl/core/include/buffers.h"
32 
33 #include "Poco/StreamCopier.h"
34 #include "Poco/URI.h"
35 #include "Poco/Net/Context.h"
36 #include "Poco/Net/HTTPRequest.h"
37 #include "Poco/Net/HTTPSClientSession.h"
38 #include "Poco/Net/HTTPResponse.h"
39 #include "Poco/Net/SSLException.h"
40 
41 #include "consul_interface.h"
42 #include "service.h"
43 
44 #ifndef AOSSL_CONSUL_INCLUDE_CONSUL_ADMIN_H_
45 #define AOSSL_CONSUL_INCLUDE_CONSUL_ADMIN_H_
46 
47 namespace AOSSL {
48 
49 // Consul Admin
50 
51 // The Consul Administrator, which handles distributed configuration
52 // & service discovery
53 
54 // This relies on the HTTP Administrator, and takes in a Service object in
55 // order to register. It's responses are JSON strings that are recieved
56 // from Consul. Note that the values returned from the Key-Value store
57 // will be stored in base64 format
59  inline static bool is_base64(unsigned char c) {
60  return (isalnum(c) || (c == '+') || (c == '/'));
61  }
62  public:
63  // Base64 decoding function for accessing KV Store values
64  void base64_decode_by_reference(std::string const& encoded_string, StringBuffer& ret_buffer);
65  StringBuffer* base64_decode_safe(std::string const& encoded_string);
66 
67  // Add an ACL Token to the Consul Admin
68  inline void add_acl_token(std::string& token) {
69  BaseHttpClient::set_acl_token(std::string("X-Consul-Token"), token);
70  }
71 
72  // Construct a consul admin, passing in the connection string
73  ConsulAdmin(std::string& caddr) : BaseHttpClient(caddr, 5) {}
74 
75  // Construct a consul admin, passing in the connection string and timeout
76  ConsulAdmin(std::string& caddr, int tout) : BaseHttpClient(caddr, tout) {}
77 
78  // Construct a consul admin, passing in the connection string and ssl cert
79  ConsulAdmin(std::string& caddr, std::string& cert) : \
80  BaseHttpClient(caddr, 5, cert) {}
81 
82  // Construct a consul admin, passing in the connection string, timeout, and ssl cert
83  ConsulAdmin(std::string& caddr, int tout, std::string& cert) : \
84  BaseHttpClient(caddr, tout, cert) {}
85 
86  // Construct a consul admin, passing in the connection string, timeout,
87  // ssl cert, and consul acl token
88  ConsulAdmin(std::string& caddr, int tout, std::string& cert, std::string& token) : \
89  BaseHttpClient(caddr, tout, cert) {add_acl_token(token);}
90 
91  // Delete a consul admin
92  ~ConsulAdmin() {}
93 
94  // Service Registry Functions
95 
96  // Register the Service
97  bool register_service(const ServiceInterface& s);
98 
99  // Deregister the Service
100  bool deregister_service(const ServiceInterface& s);
101 
102  // Configuration Key-Value Storage Functions
103 
104  // Set a configuration value.
105 
106  // If the key does not exist, then this will add it.
107  // Otherwise, it will update the existing key.
108  bool set_config_value(std::string key, std::string val);
109 
110  // Delete a configuration value
111  bool del_config_value(std::string key);
112 
113  // Basic Queries
114  // All Return a JSON string
115 
116  // Local Agent Queries
117 
118  // Query the local agent for services registered
120 
121  // Query the local agent for it's info
123 
124  // Query for healthy services only
126 
127  // Catalog Queries
128 
129  // Query the catalog for datacenters
131 
132  // Query the catalog for the nodes in a particular datacenter
133  StringBuffer* nodes_dc(std::string data_center);
134 
135  // Query the catalog for the services in a particular datacenter
136  StringBuffer* services_dc(std::string data_center);
137 
138  // Query the catalog for the nodes running a particular service
139  StringBuffer* nodes_service(std::string service);
140 
141  // Query the catalog for the services provided by a particular node
142  StringBuffer* \
143  services_node(std::string node, std::string data_center);
144 
145  // Implementations of KeyValueStore interface
147  inline bool opt_exist(std::string key) {
148  std::string url = "/v1/kv/";
149  url = url.append(key);
150  StringBuffer buf;
152  return buf.success;
153  }
154 
156  inline StringBuffer* get_opt(std::string key) {
157  std::string url = "/v1/kv/";
158  url = url.append(key);
159  StringBuffer *buf = new StringBuffer;
161  return buf;
162  }
163 
165  inline void get_opt(std::string key, StringBuffer& val) {
166  std::string url = "/v1/kv/";
167  url = url.append(key);
169  }
170 
171  // Empty as getting any key always gets the most up-to-date
172  // so we don't need to load our configuration repeatedly
173  inline void load_config() {
174 
175  }
176 };
177 
178 }
179 
180 #endif // AOSSL_CONSUL_INCLUDE_CONSUL_ADMIN_H_
StringBuffer * services_dc(std::string data_center)
Query the catalog for the services in a particular datacenter.
StringBuffer * agent_info()
Query the local agent for it&#39;s info.
A Structure for holding a single value.
Definition: buffers.h:42
void get_opt(std::string key, StringBuffer &val)
Get an option by key.
Definition: consul_admin.h:165
StringBuffer * healthy_services()
Query for healthy services only.
StringBuffer * nodes_dc(std::string data_center)
Query the catalog for the nodes in a particular datacenter.
bool set_config_value(std::string key, std::string val)
Set a configuration value.
bool del_config_value(std::string key)
Delete a configuration value.
BaseHttpClient(std::string &vaddr, int tout, std::string &cert)
Construct a secured HTTP Client.
Definition: base_http_client.h:123
StringBuffer * services()
Query the local agent for services registered.
Definition: consul_admin.h:58
StringBuffer * base64_decode_safe(std::string const &encoded_string)
Convinience Method for base64 decoding.
The Consul Administrator, who handles configuration & service discovery.
Definition: consul_interface.h:115
void set_acl_token(std::string token_name, std::string new_token)
Add an ACL Token.
Definition: base_http_client.h:160
A Service class which can be registered with Consul for each app instance.
Definition: consul_interface.h:57
StringBuffer * nodes_service(std::string service)
Query the catalog for the nodes running a particular service.
bool success
Success flag.
Definition: buffers.h:35
bool register_service(const ServiceInterface &s)
Register the Service.
void get_by_reference(std::string &query_url, AOSSL::StringBuffer &ret_buffer)
Execute a Get query.
Definition: base_http_client.h:148
StringBuffer * datacenters()
Query the catalog for datacenters.
void load_config()
Re-load configuration.
Definition: consul_admin.h:173
StringBuffer * get_opt(std::string key)
Get an option by key.
Definition: consul_admin.h:156
StringBuffer * services_node(std::string node, std::string data_center)
Query the catalog for the services provided by a particular node.
void base64_decode_by_reference(std::string const &encoded_string, StringBuffer &ret_buffer)
Convinience Method for base64 decoding.
bool opt_exist(std::string key)
Does a key exist?
Definition: consul_admin.h:147
bool deregister_service(const ServiceInterface &s)
Deregister the Service.
Base HTTP Client.
Definition: base_http_client.h:46
void add_acl_token(std::string &token)
Add an ACL Token to the Consul Admin.
Definition: consul_admin.h:68
Definition: cli.h:35