Skip to content

Commit aed29d2

Browse files
authored
feat: allow user to specify domain in container labels in order to identify it (#198)
* feat: allow user to specify domain in container labels in order to identify it * refactor: remove port from domain before getting container
1 parent 3397e2a commit aed29d2

4 files changed

Lines changed: 23 additions & 24 deletions

File tree

internal/docker/docker.go

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func (docker *Docker) DockerConnected() bool {
7474
return err == nil
7575
}
7676

77-
func (docker *Docker) GetLabels(appId string) (types.Labels, error) {
77+
func (docker *Docker) GetLabels(id string, domain string) (types.Labels, error) {
7878
// Check if we have access to the Docker API
7979
isConnected := docker.DockerConnected()
8080

@@ -85,15 +85,16 @@ func (docker *Docker) GetLabels(appId string) (types.Labels, error) {
8585
}
8686

8787
// Get the containers
88+
log.Debug().Msg("Getting containers")
89+
8890
containers, err := docker.GetContainers()
8991

9092
// If there is an error, return false
9193
if err != nil {
94+
log.Error().Err(err).Msg("Error getting containers")
9295
return types.Labels{}, err
9396
}
9497

95-
log.Debug().Msg("Got containers")
96-
9798
// Loop through the containers
9899
for _, container := range containers {
99100
// Inspect the container
@@ -105,28 +106,22 @@ func (docker *Docker) GetLabels(appId string) (types.Labels, error) {
105106
continue
106107
}
107108

108-
// Get the container name (for some reason it is /name)
109-
containerName := strings.TrimPrefix(inspect.Name, "/")
110-
111-
// There is a container with the same name as the app ID
112-
if containerName == appId {
113-
log.Debug().Str("container", containerName).Msg("Found container")
109+
// Get the labels
110+
log.Debug().Str("id", inspect.ID).Msg("Getting labels for container")
114111

115-
// Get only the tinyauth labels in a struct
116-
labels, err := utils.GetLabels(inspect.Config.Labels)
112+
labels, err := utils.GetLabels(inspect.Config.Labels)
117113

118-
// Check if there was an error
119-
if err != nil {
120-
log.Error().Err(err).Msg("Error parsing labels")
121-
return types.Labels{}, err
122-
}
123-
124-
log.Debug().Msg("Got labels")
114+
// Check if there was an error
115+
if err != nil {
116+
log.Warn().Str("id", container.ID).Err(err).Msg("Error getting container labels, skipping")
117+
continue
118+
}
125119

126-
// Return labels
120+
// Check if the labels match the id or the domain
121+
if strings.TrimPrefix(inspect.Name, "/") == id || labels.Domain == domain {
122+
log.Debug().Str("id", inspect.ID).Msg("Found matching container")
127123
return labels, nil
128124
}
129-
130125
}
131126

132127
log.Debug().Msg("No matching container found, returning empty labels")

internal/handlers/handlers.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,14 @@ func (h *Handlers) AuthHandler(c *gin.Context) {
6969
proto := c.Request.Header.Get("X-Forwarded-Proto")
7070
host := c.Request.Header.Get("X-Forwarded-Host")
7171

72-
// Get the app id
73-
appId := strings.Split(host, ".")[0]
72+
// Remove the port from the host if it exists
73+
hostPortless := strings.Split(host, ":")[0] // *lol*
74+
75+
// Get the id
76+
id := strings.Split(hostPortless, ".")[0]
7477

7578
// Get the container labels
76-
labels, err := h.Docker.GetLabels(appId)
79+
labels, err := h.Docker.GetLabels(id, hostPortless)
7780

7881
log.Debug().Interface("labels", labels).Msg("Got labels")
7982

internal/types/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,6 @@ type Labels struct {
104104
Users string
105105
Allowed string
106106
Headers []string
107+
Domain string
107108
OAuth OAuthLabels
108109
}

internal/utils/utils.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func GetLabels(labels map[string]string) (types.Labels, error) {
201201
var labelsParsed types.Labels
202202

203203
// Decode the labels into the labels struct
204-
err := parser.Decode(labels, &labelsParsed, "tinyauth", "tinyauth.users", "tinyauth.allowed", "tinyauth.headers", "tinyauth.oauth")
204+
err := parser.Decode(labels, &labelsParsed, "tinyauth", "tinyauth.users", "tinyauth.allowed", "tinyauth.headers", "tinyauth.domain", "tinyauth.oauth")
205205

206206
// Check if there was an error
207207
if err != nil {

0 commit comments

Comments
 (0)