From 2b2323fae773daf6a5772294a7004bd540348ebd Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Wed, 28 Dec 2022 16:02:16 +0100 Subject: [PATCH] outposts: include hostname in outpost heartbeat Signed-off-by: Jens Langhammer --- authentik/outposts/api/outposts.py | 14 +++++++++++++- authentik/outposts/channels.py | 1 + authentik/outposts/models.py | 1 + internal/outpost/ak/api.go | 13 +++++++++++++ internal/outpost/ak/api_ws.go | 12 ++---------- schema.yml | 8 ++++++++ 6 files changed, 38 insertions(+), 11 deletions(-) diff --git a/authentik/outposts/api/outposts.py b/authentik/outposts/api/outposts.py index cc6fae88c..0b7091e11 100644 --- a/authentik/outposts/api/outposts.py +++ b/authentik/outposts/api/outposts.py @@ -19,7 +19,13 @@ from authentik.core.api.utils import PassiveSerializer, is_dict from authentik.core.models import Provider from authentik.outposts.api.service_connections import ServiceConnectionSerializer from authentik.outposts.apps import MANAGED_OUTPOST -from authentik.outposts.models import Outpost, OutpostConfig, OutpostType, default_outpost_config +from authentik.outposts.models import ( + Outpost, + OutpostConfig, + OutpostState, + OutpostType, + default_outpost_config, +) from authentik.providers.ldap.models import LDAPProvider from authentik.providers.proxy.models import ProxyProvider @@ -96,6 +102,7 @@ class OutpostDefaultConfigSerializer(PassiveSerializer): class OutpostHealthSerializer(PassiveSerializer): """Outpost health status""" + uid = CharField(read_only=True) last_seen = DateTimeField(read_only=True) version = CharField(read_only=True) version_should = CharField(read_only=True) @@ -105,6 +112,8 @@ class OutpostHealthSerializer(PassiveSerializer): build_hash = CharField(read_only=True, required=False) build_hash_should = CharField(read_only=True, required=False) + hostname = CharField(read_only=True, required=False) + class OutpostFilter(FilterSet): """Filter for Outposts""" @@ -145,13 +154,16 @@ class OutpostViewSet(UsedByMixin, ModelViewSet): outpost: Outpost = self.get_object() states = [] for state in outpost.state: + state: OutpostState states.append( { + "uid": state.uid, "last_seen": state.last_seen, "version": state.version, "version_should": state.version_should, "version_outdated": state.version_outdated, "build_hash": state.build_hash, + "hostname": state.hostname, "build_hash_should": get_build_hash(), } ) diff --git a/authentik/outposts/channels.py b/authentik/outposts/channels.py index 2deaff92b..ffa5e97d6 100644 --- a/authentik/outposts/channels.py +++ b/authentik/outposts/channels.py @@ -98,6 +98,7 @@ class OutpostConsumer(AuthJsonConsumer): if self.channel_name not in state.channel_ids: state.channel_ids.append(self.channel_name) state.last_seen = datetime.now() + state.hostname = msg.args.get("hostname", "") if not self.first_msg: GAUGE_OUTPOSTS_CONNECTED.labels( diff --git a/authentik/outposts/models.py b/authentik/outposts/models.py index 4cac05ff3..f3739231e 100644 --- a/authentik/outposts/models.py +++ b/authentik/outposts/models.py @@ -431,6 +431,7 @@ class OutpostState: version: Optional[str] = field(default=None) version_should: Version | LegacyVersion = field(default=OUR_VERSION) build_hash: str = field(default="") + hostname: str = field(default="") _outpost: Optional[Outpost] = field(default=None) diff --git a/internal/outpost/ak/api.go b/internal/outpost/ak/api.go index 8816247b9..52f2af282 100644 --- a/internal/outpost/ak/api.go +++ b/internal/outpost/ak/api.go @@ -167,6 +167,19 @@ func (a *APIController) OnRefresh() error { return err } +func (a *APIController) getWebsocketArgs() map[string]interface{} { + args := map[string]interface{}{ + "version": constants.VERSION, + "buildHash": constants.BUILD("tagged"), + "uuid": a.instanceUUID.String(), + } + hostname, err := os.Hostname() + if err == nil { + args["hostname"] = hostname + } + return args +} + func (a *APIController) StartBackgroundTasks() error { OutpostInfo.With(prometheus.Labels{ "outpost_name": a.Outpost.Name, diff --git a/internal/outpost/ak/api_ws.go b/internal/outpost/ak/api_ws.go index 2cb084a62..9d8d0d265 100644 --- a/internal/outpost/ak/api_ws.go +++ b/internal/outpost/ak/api_ws.go @@ -49,11 +49,7 @@ func (ac *APIController) initWS(akURL url.URL, outpostUUID string) error { // Send hello message with our version msg := websocketMessage{ Instruction: WebsocketInstructionHello, - Args: map[string]interface{}{ - "version": constants.VERSION, - "buildHash": constants.BUILD("tagged"), - "uuid": ac.instanceUUID.String(), - }, + Args: ac.getWebsocketArgs(), } err = ws.WriteJSON(msg) if err != nil { @@ -163,11 +159,7 @@ func (ac *APIController) startWSHealth() { for ; true; <-ticker.C { aliveMsg := websocketMessage{ Instruction: WebsocketInstructionHello, - Args: map[string]interface{}{ - "version": constants.VERSION, - "buildHash": constants.BUILD("tagged"), - "uuid": ac.instanceUUID.String(), - }, + Args: ac.getWebsocketArgs(), } if ac.wsConn == nil { go ac.reconnectWS() diff --git a/schema.yml b/schema.yml index c44b70fdc..631bbcf02 100644 --- a/schema.yml +++ b/schema.yml @@ -30179,6 +30179,9 @@ components: type: object description: Outpost health status properties: + uid: + type: string + readOnly: true last_seen: type: string format: date-time @@ -30198,10 +30201,15 @@ components: build_hash_should: type: string readOnly: true + hostname: + type: string + readOnly: true required: - build_hash - build_hash_should + - hostname - last_seen + - uid - version - version_outdated - version_should