Merge ~woutervb/charm-graylog:bug/1818615 into ~graylog-charmers/charm-graylog:master

Proposed by Wouter van Bommel
Status: Superseded
Proposed branch: ~woutervb/charm-graylog:bug/1818615
Merge into: ~graylog-charmers/charm-graylog:master
Diff against target: 97 lines (+42/-7)
3 files modified
config.yaml (+6/-3)
lib/charms/layer/graylog/utils.py (+32/-0)
reactive/graylog.py (+4/-4)
Reviewer Review Type Date Requested Status
Xav Paice (community) Needs Fixing
Canonical IS Reviewers Pending
Review via email: mp+379141@code.launchpad.net

This proposal has been superseded by a proposal from 2020-02-25.

Commit message

Determine default network interface

To post a comment you must log in.
Revision history for this message
🤖 Canonical IS Merge Bot (canonical-is-mergebot) wrote :

This merge proposal is being monitored by mergebot. Change the status to Approved to merge.

Revision history for this message
Haw Loeung (hloeung) :
Revision history for this message
Xav Paice (xavpaice) wrote :

couple of diff comments, plus Haw's review

review: Needs Fixing

Unmerged commits

909907e... by Wouter van Bommel

Determine default network interface

The behaviour around the rest_transport_uri has been changed in such a
way that it now is allowed to specify 0.0.0.0 as the default address.
This will trigger a piece of code that will substitute this address with
the ip address connected to the default gateway interface.
This should give the sane results in most situations, while it still
allows to be overridden.

This commit fixes bug: lp:1818615

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/config.yaml b/config.yaml
2index d0d2649..4ac707e 100644
3--- a/config.yaml
4+++ b/config.yaml
5@@ -35,16 +35,19 @@ options:
6 Example: http://10.0.0.1:9000/
7 rest_transport_uri:
8 type: string
9- default: ""
10+ default: "http://0.0.0.0:9001/api/"
11 description: |
12 If set, this will be promoted in the cluster discovery APIs. You will need
13 to define this if your Graylog server is running behind a HTTP proxy that
14- is rewriting the scheme, host name, or URI. This must not contain a
15- wildcard address (0.0.0.0).
16+ is rewriting the scheme, host name, or URI.
17
18 For Graylog 2, this usually takes the form http://10.0.0.1:9001/api/.
19 For Graylog 3 and higher, this is known as the 'http_publish_uri' and looks
20 like http://10.0.0.1:9000/.
21+
22+ If the ip address 0.0.0.0 is specified, the charm will attempt to deduct
23+ the network interface that is used for connections to the default gateway and
24+ use the associated ip address.
25 index_replicas:
26 type: int
27 default: 0
28diff --git a/lib/charms/layer/graylog/utils.py b/lib/charms/layer/graylog/utils.py
29index cf89b61..a3e35bd 100644
30--- a/lib/charms/layer/graylog/utils.py
31+++ b/lib/charms/layer/graylog/utils.py
32@@ -1,3 +1,6 @@
33+import socket
34+from urllib.parse import urlparse
35+
36 from charmhelpers.core import hookenv
37 from charms.layer import snap
38
39@@ -39,4 +42,33 @@ def validate_api_uri(uri):
40 hookenv.log("Removing 'api/' from the configurted REST API options")
41 uri = uri[:-4]
42
43+ parsed_url = urlparse(uri)
44+ if parsed_url.hostname == '0.0.0.0':
45+ uri = parsed_url._replace(netloc='%s:%s' % (get_ip_address(),
46+ parsed_url.port))\
47+ .geturl()
48+
49 return uri
50+
51+
52+def get_ip_address():
53+ """Determine the ip address that would be used for outgoing connection.
54+
55+ This function tries to determine the network ip address, that would be used
56+ when access servers in the internet. It uses a dummy UDP connection todo
57+ so.
58+ As the connection itself is not made, it is safe to use any address.
59+ """
60+
61+ # Start a socket via ipv4, using UDP
62+ sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
63+
64+ # Make dummy attempt to ntp servers from the pool.ntp.org, we hard code one
65+ # of them
66+
67+ sock.connect(("220.158.215.21", 123))
68+
69+ ip = sock.getsockname()[0]
70+ sock.close()
71+ print('Found ip [%s] to use' % ip)
72+ return ip
73diff --git a/reactive/graylog.py b/reactive/graylog.py
74index b26ad96..0dead17 100644
75--- a/reactive/graylog.py
76+++ b/reactive/graylog.py
77@@ -155,8 +155,8 @@ def configure_graylog():
78 db.set('web_listen_port', url.port)
79 hookenv.open_port(url.port)
80
81- if conf['rest_transport_uri']:
82- set_conf('rest_transport_uri', validate_api_uri(conf['rest_transport_uri']))
83+ # We always call set this, so it gets a sensible default
84+ set_conf('rest_transport_uri', validate_api_uri(conf['rest_transport_uri']))
85
86 if conf['web_endpoint_uri']:
87 set_conf('web_endpoint_uri', validate_api_uri(conf['web_endpoint_uri']))
88@@ -164,8 +164,8 @@ def configure_graylog():
89 if conf['web_listen_uri']:
90 url = urlparse(conf['web_listen_uri'])
91 set_conf('http_bind_address', url.netloc)
92- if conf['rest_transport_uri']:
93- set_conf('http_publish_uri', validate_api_uri(conf['rest_transport_uri']))
94+ # We always call set this, so it gets a sensible default
95+ set_conf('http_publish_uri', validate_api_uri(conf['rest_transport_uri']))
96 if conf['web_endpoint_uri']:
97 set_conf('http_external_uri', validate_api_uri(conf['web_endpoint_uri']))
98

Subscribers

People subscribed via source and target branches