Skip to content

Commit be10491

Browse files
committed
Extend SSL/TLS support to st2stream and st2api
1 parent 8513165 commit be10491

File tree

6 files changed

+70
-6
lines changed

6 files changed

+70
-6
lines changed

st2api/st2api/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ def setup_app(config=None):
4747
"name": "api",
4848
"listen_host": cfg.CONF.api.host,
4949
"listen_port": cfg.CONF.api.port,
50+
"listen_ssl": cfg.CONF.api.use_ssl,
5051
"type": "active",
5152
}
5253

st2api/st2api/cmd/api.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ def _setup():
5454
"name": "api",
5555
"listen_host": cfg.CONF.api.host,
5656
"listen_port": cfg.CONF.api.port,
57+
"listen_ssl": cfg.CONF.api.use_ssl,
5758
"type": "active",
5859
}
5960

@@ -76,13 +77,34 @@ def _setup():
7677
def _run_server():
7778
host = cfg.CONF.api.host
7879
port = cfg.CONF.api.port
80+
use_ssl = cfg.CONF.api.use_ssl
7981

80-
LOG.info("(PID=%s) ST2 API is serving on http://%s:%s.", os.getpid(), host, port)
82+
cert_file_path = os.path.realpath(cfg.CONF.api.cert)
83+
key_file_path = os.path.realpath(cfg.CONF.api.key)
84+
85+
if use_ssl and not os.path.isfile(cert_file_path):
86+
raise ValueError('Certificate file "%s" doesn\'t exist' % (cert_file_path))
87+
88+
if use_ssl and not os.path.isfile(key_file_path):
89+
raise ValueError('Private key file "%s" doesn\'t exist' % (key_file_path))
90+
91+
LOG.info(
92+
"(PID=%s) ST2 API is serving on %s://%s:%s.",
93+
os.getpid(),
94+
"https" if use_ssl else "http",
95+
host,
96+
port,
97+
)
8198

8299
max_pool_size = eventlet.wsgi.DEFAULT_MAX_SIMULTANEOUS_REQUESTS
83100
worker_pool = eventlet.GreenPool(max_pool_size)
84101
sock = eventlet.listen((host, port))
85102

103+
if use_ssl:
104+
sock = eventlet.wrap_ssl(
105+
sock, certfile=cert_file_path, keyfile=key_file_path, server_side=True
106+
)
107+
86108
wsgi.server(
87109
sock, app.setup_app(), custom_pool=worker_pool, log=LOG, log_output=False
88110
)

st2api/st2api/config.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def _register_app_opts(ignore_errors=False):
7676
pecan_opts, group="api_pecan", ignore_errors=ignore_errors
7777
)
7878

79-
logging_opts = [
79+
api_opts = [
8080
cfg.BoolOpt("debug", default=False),
8181
cfg.StrOpt(
8282
"logging",
@@ -89,8 +89,17 @@ def _register_app_opts(ignore_errors=False):
8989
help="Maximum limit (page size) argument which can be "
9090
"specified by the user in a query string.",
9191
),
92+
cfg.BoolOpt("use_ssl", default=False, help="Specify to enable SSL / TLS mode"),
93+
cfg.StrOpt(
94+
"cert",
95+
default="/etc/apache2/ssl/mycert.crt",
96+
help='Path to the SSL certificate file. Only used when "use_ssl" is specified.',
97+
),
98+
cfg.StrOpt(
99+
"key",
100+
default="/etc/apache2/ssl/mycert.key",
101+
help='Path to the SSL private key file. Only used when "use_ssl" is specified.',
102+
),
92103
]
93104

94-
common_config.do_register_opts(
95-
logging_opts, group="api", ignore_errors=ignore_errors
96-
)
105+
common_config.do_register_opts(api_opts, group="api", ignore_errors=ignore_errors)

st2stream/st2stream/app.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def setup_app(config={}):
5252
"name": "stream",
5353
"listen_host": cfg.CONF.stream.host,
5454
"listen_port": cfg.CONF.stream.port,
55+
"listen_ssl": cfg.CONF.stream.use_ssl,
5556
"type": "active",
5657
}
5758
# This should be called in gunicorn case because we only want

st2stream/st2stream/cmd/api.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ def _setup():
6060
"name": "stream",
6161
"listen_host": cfg.CONF.stream.host,
6262
"listen_port": cfg.CONF.stream.port,
63+
"listen_ssl": cfg.CONF.stream.use_ssl,
6364
"type": "active",
6465
}
6566
common_setup(
@@ -78,15 +79,34 @@ def _setup():
7879
def _run_server():
7980
host = cfg.CONF.stream.host
8081
port = cfg.CONF.stream.port
82+
use_ssl = cfg.CONF.stream.use_ssl
83+
84+
cert_file_path = os.path.realpath(cfg.CONF.stream.cert)
85+
key_file_path = os.path.realpath(cfg.CONF.stream.key)
86+
87+
if use_ssl and not os.path.isfile(cert_file_path):
88+
raise ValueError('Certificate file "%s" doesn\'t exist' % (cert_file_path))
89+
90+
if use_ssl and not os.path.isfile(key_file_path):
91+
raise ValueError('Private key file "%s" doesn\'t exist' % (key_file_path))
8192

8293
LOG.info(
83-
"(PID=%s) ST2 Stream API is serving on http://%s:%s.", os.getpid(), host, port
94+
"(PID=%s) ST2 Stream API is serving on %s://%s:%s.",
95+
os.getpid(),
96+
"https" if use_ssl else "http",
97+
host,
98+
port,
8499
)
85100

86101
max_pool_size = eventlet.wsgi.DEFAULT_MAX_SIMULTANEOUS_REQUESTS
87102
worker_pool = eventlet.GreenPool(max_pool_size)
88103
sock = eventlet.listen((host, port))
89104

105+
if use_ssl:
106+
sock = eventlet.wrap_ssl(
107+
sock, certfile=cert_file_path, keyfile=key_file_path, server_side=True
108+
)
109+
90110
def queue_shutdown(signal_number, stack_frame):
91111
deregister_service(STREAM)
92112
eventlet.spawn_n(

st2stream/st2stream/config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ def _register_app_opts(ignore_errors=False):
6666
default="/etc/st2/logging.stream.conf",
6767
help="location of the logging.conf file",
6868
),
69+
cfg.BoolOpt("use_ssl", default=False, help="Specify to enable SSL / TLS mode"),
70+
cfg.StrOpt(
71+
"cert",
72+
default="/etc/apache2/ssl/mycert.crt",
73+
help='Path to the SSL certificate file. Only used when "use_ssl" is specified.',
74+
),
75+
cfg.StrOpt(
76+
"key",
77+
default="/etc/apache2/ssl/mycert.key",
78+
help='Path to the SSL private key file. Only used when "use_ssl" is specified.',
79+
),
6980
]
7081

7182
common_config.do_register_opts(

0 commit comments

Comments
 (0)