-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support insecure TLS and only CA cert for Elasticsearch #1918
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,5 @@ cmd/docs/*.1 | |
cmd/docs/*.yaml | ||
crossdock/crossdock-* | ||
run-crossdock.log | ||
|
||
__pycache__ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,7 @@ def main(): | |
print('ES_TLS_CA ... Path to TLS CA file.') | ||
print('ES_TLS_CERT ... Path to TLS certificate file.') | ||
print('ES_TLS_KEY ... Path to TLS key file.') | ||
print('ES_TLS_SKIP_HOST_VERIFY ... (insecure) Skip server\'s certificate chain and host name verification.') | ||
print('ES_VERSION ... The major Elasticsearch version. If not specified, the value will be auto-detected from Elasticsearch.') | ||
print('init configuration:') | ||
print('\tSHARDS ... the number of shards per index in Elasticsearch (default {}).'.format(SHARDS)) | ||
|
@@ -46,18 +47,7 @@ def main(): | |
print('\tUNIT_COUNT ... count of UNITs (default {}).'.format(UNIT_COUNT)) | ||
sys.exit(1) | ||
|
||
username = os.getenv("ES_USERNAME") | ||
password = os.getenv("ES_PASSWORD") | ||
|
||
if username is not None and password is not None: | ||
client = elasticsearch.Elasticsearch(sys.argv[2:], http_auth=(username, password)) | ||
elif str2bool(os.getenv("ES_TLS", 'false')): | ||
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=os.getenv("ES_TLS_CA")) | ||
context.load_cert_chain(certfile=os.getenv("ES_TLS_CERT"), keyfile=os.getenv("ES_TLS_KEY")) | ||
client = elasticsearch.Elasticsearch(sys.argv[2:], ssl_context=context) | ||
else: | ||
client = elasticsearch.Elasticsearch(sys.argv[2:]) | ||
|
||
client = create_client(os.getenv("ES_USERNAME"), os.getenv("ES_PASSWORD"), str2bool(os.getenv("ES_TLS", 'false')), os.getenv("ES_TLS_CA"), os.getenv("ES_TLS_CERT"), os.getenv("ES_TLS_KEY"), str2bool(os.getenv("ES_TLS_SKIP_HOST_VERIFY", 'false'))) | ||
prefix = os.getenv('INDEX_PREFIX', '') | ||
if prefix != '': | ||
prefix += '-' | ||
|
@@ -107,7 +97,7 @@ def perform_action(action, client, write_alias, read_alias, index_to_rollover, t | |
def create_index_template(template, template_name): | ||
print('Creating index template {}'.format(template_name)) | ||
headers = {'Content-Type': 'application/json'} | ||
s = get_request_session(os.getenv("ES_USERNAME"), os.getenv("ES_PASSWORD"), str2bool(os.getenv("ES_TLS", 'false')), os.getenv("ES_TLS_CA"), os.getenv("ES_TLS_CERT"), os.getenv("ES_TLS_KEY")) | ||
s = get_request_session(os.getenv("ES_USERNAME"), os.getenv("ES_PASSWORD"), str2bool(os.getenv("ES_TLS", 'false')), os.getenv("ES_TLS_CA"), os.getenv("ES_TLS_CERT"), os.getenv("ES_TLS_KEY"), os.getenv("ES_TLS_SKIP_HOST_VERIFY", 'false')) | ||
r = s.put(sys.argv[2] + '/_template/' + template_name, headers=headers, data=template) | ||
print(r.text) | ||
r.raise_for_status() | ||
|
@@ -202,8 +192,12 @@ def empty_list(ilo, error_msg): | |
sys.exit(0) | ||
|
||
|
||
def get_request_session(username, password, tls, ca, cert, key): | ||
def get_request_session(username, password, tls, ca, cert, key, skipHostVerify): | ||
session = requests.Session() | ||
if ca is not None: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: should this be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. good point I think they are mutually exclusive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have changed it a bit. if CA is provided it will use that in the precedence of skip verify. I want to avoid accidentally using insecure connections. |
||
session.verify = ca | ||
elif skipHostVerify: | ||
session.verify = False | ||
if username is not None and password is not None: | ||
session.auth = HTTPBasicAuth(username, password) | ||
elif tls: | ||
|
@@ -221,6 +215,22 @@ def get_version(client): | |
return esVersion | ||
|
||
|
||
def create_client(username, password, tls, ca, cert, key, skipHostVerify): | ||
context = ssl.create_default_context() | ||
if ca is not None: | ||
context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, cafile=ca) | ||
elif skipHostVerify: | ||
context.check_hostname = False | ||
context.verify_mode = ssl.CERT_NONE | ||
if username is not None and password is not None: | ||
return elasticsearch.Elasticsearch(sys.argv[2:], http_auth=(username, password), ssl_context=context) | ||
elif tls: | ||
context.load_cert_chain(certfile=cert, keyfile=key) | ||
return elasticsearch.Elasticsearch(sys.argv[2:], ssl_context=context) | ||
else: | ||
return elasticsearch.Elasticsearch(sys.argv[2:], ssl_context=context) | ||
|
||
|
||
if __name__ == "__main__": | ||
logging.getLogger().setLevel(logging.DEBUG) | ||
main() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when no TLS is involved at all, would non-nill
TLSClientConfig
cause issues?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should not godoc from
http.Transport.TLSClientConfig
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this godoc doesn't seem to clarify what happens to tls / no-tls. Perhaps it's just driven off the http scheme