Skip to content

Commit 82afd89

Browse files
committed
Docker image for username service
Docker image for the username service used to generate fedid accounts on VMs
1 parent 72decce commit 82afd89

File tree

6 files changed

+239
-0
lines changed

6 files changed

+239
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
name: Usrename Service
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
paths:
9+
- ".github/workflows/username_service.yaml"
10+
- "stfc-username-service/**"
11+
12+
jobs:
13+
push_dev_image_harbor:
14+
runs-on: ubuntu-latest
15+
steps:
16+
- uses: actions/checkout@v5
17+
18+
- name: Set up Docker Buildx
19+
uses: docker/setup-buildx-action@v3
20+
21+
- name: Login to Harbor
22+
uses: docker/login-action@v3
23+
with:
24+
registry: harbor.stfc.ac.uk
25+
username: ${{ secrets.STAGING_HARBOR_USERNAME }}
26+
password: ${{ secrets.STAGING_HARBOR_TOKEN }}
27+
28+
- name: Set commit SHA for later
29+
id: commit_sha
30+
run: echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
31+
32+
- name: Build and push to staging project
33+
uses: docker/build-push-action@v6
34+
with:
35+
cache-from: type=gha
36+
cache-to: type=gha,mode=max
37+
push: true
38+
context: "{{defaultContext}}:stfc-username-service"
39+
tags: "harbor.stfc.ac.uk/stfc-cloud-staging/stfc-username-service:${{ steps.commit_sha.outputs.sha_short }}"
40+
41+
- name: Inform of tagged name
42+
run: echo "Image published to harbor.stfc.ac.uk/stfc-cloud-staging/stfc-username-service:${{ steps.commit_sha.outputs.sha_short }}"
43+
44+
push_release_image_harbor:
45+
runs-on: ubuntu-latest
46+
if: github.ref == 'refs/heads/master'
47+
steps:
48+
- uses: actions/checkout@v5
49+
50+
- name: Set up Docker Buildx
51+
uses: docker/setup-buildx-action@v3
52+
53+
- name: Login to Harbor
54+
uses: docker/login-action@v3
55+
with:
56+
registry: harbor.stfc.ac.uk
57+
username: ${{ secrets.HARBOR_USERNAME }}
58+
password: ${{ secrets.HARBOR_TOKEN }}
59+
60+
- name: Get release tag for later
61+
id: release_tag
62+
run: echo "version=$(cat stfc-username-service/version.txt)" >> $GITHUB_OUTPUT
63+
64+
- name: Check if release file has updated
65+
uses: dorny/paths-filter@v3
66+
id: release_updated
67+
with:
68+
filters: |
69+
version:
70+
- 'stfc-username-service/version.txt'
71+
72+
- name: Build and push on version change
73+
uses: docker/build-push-action@v6
74+
if: steps.release_updated.outputs.version == 'true'
75+
with:
76+
cache-from: type=gha
77+
cache-to: type=gha,mode=max
78+
push: true
79+
context: "{{defaultContext}}:stfc-username-service"
80+
tags: "harbor.stfc.ac.uk/stfc-cloud/stfc-username-service:v${{ steps.release_tag.outputs.version }}"
81+
82+
- name: Inform of tagged name
83+
if: steps.release_updated.outputs.version == 'true'
84+
run: echo "Image published to harbor.stfc.ac.uk/stfc-cloud/stfc-username-service:v${{ steps.release_tag.outputs.version }}"

stfc-username-service/Dockerfile

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
FROM golang:1.25-alpine AS BuildStage
2+
WORKDIR /app
3+
COPY . .
4+
RUN go mod download && go mod verify
5+
RUN go build -o username .
6+
7+
FROM alpine:latest AS FinalStage
8+
WORKDIR /
9+
COPY --from=BuildStage /app/username /username
10+
11+
RUN addgroup -S appgroup && adduser -S appuser -G appgroup
12+
RUN apk add --no-cache tini
13+
14+
USER appuser
15+
ENTRYPOINT ["/sbin/tini", "--"]
16+
CMD ["./username"]

stfc-username-service/go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module stfc-cloud/username
2+
3+
go 1.25.1
4+
5+
require (
6+
github.com/gophercloud/gophercloud v1.14.1
7+
github.com/gophercloud/utils v0.0.0-20231010081019-80377eca5d56
8+
)
9+
10+
require (
11+
github.com/hashicorp/go-uuid v1.0.3 // indirect
12+
github.com/mitchellh/go-homedir v1.1.0 // indirect
13+
golang.org/x/sys v0.30.0 // indirect
14+
golang.org/x/text v0.4.0 // indirect
15+
gopkg.in/yaml.v2 v2.4.0 // indirect
16+
)

stfc-username-service/go.sum

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
github.com/gophercloud/gophercloud v1.3.0/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
2+
github.com/gophercloud/gophercloud v1.14.1 h1:DTCNaTVGl8/cFu58O1JwWgis9gtISAFONqpMKNg/Vpw=
3+
github.com/gophercloud/gophercloud v1.14.1/go.mod h1:aAVqcocTSXh2vYFZ1JTvx4EQmfgzxRcNupUfxZbBNDM=
4+
github.com/gophercloud/utils v0.0.0-20231010081019-80377eca5d56 h1:sH7xkTfYzxIEgzq1tDHIMKRh1vThOEOGNsettdEeLbE=
5+
github.com/gophercloud/utils v0.0.0-20231010081019-80377eca5d56/go.mod h1:VSalo4adEk+3sNkmVJLnhHoOyOYYS8sTWLG4mv5BKto=
6+
github.com/hashicorp/go-uuid v1.0.3 h1:2gKiV6YVmrJ1i2CKKa9obLvRieoRGviZFL26PcT/Co8=
7+
github.com/hashicorp/go-uuid v1.0.3/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
8+
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
9+
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
10+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
11+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
12+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
13+
golang.org/x/crypto v0.0.0-20220829220503-c86fa9a7ed90/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
14+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
15+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
16+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
17+
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
18+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
19+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
20+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
21+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
22+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
23+
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
24+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
25+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
26+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
27+
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
28+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
29+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
30+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
31+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
32+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
33+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
34+
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
35+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
36+
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
37+
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
38+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
39+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
40+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
41+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
42+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
43+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
44+
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
45+
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=

stfc-username-service/main.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log/slog"
6+
"net/http"
7+
8+
"github.com/gophercloud/gophercloud"
9+
"github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
10+
"github.com/gophercloud/gophercloud/openstack/identity/v3/users"
11+
"github.com/gophercloud/utils/openstack/clientconfig"
12+
)
13+
14+
var identityClient *gophercloud.ServiceClient
15+
var computeClient *gophercloud.ServiceClient
16+
17+
func init() {
18+
var err error
19+
opts := new(clientconfig.ClientOpts)
20+
opts.Cloud = "openstack"
21+
22+
identityClient, err = clientconfig.NewServiceClient("identity", opts)
23+
if err != nil {
24+
slog.Error("Failed to initilize identity client")
25+
panic(err)
26+
}
27+
computeClient, err = clientconfig.NewServiceClient("compute", opts)
28+
if err != nil {
29+
slog.Error("Failed to initilize compute client")
30+
panic(err)
31+
}
32+
}
33+
34+
func getServerUserID(computeClient *gophercloud.ServiceClient, serverID string) (string, error) {
35+
// Get the user ID associated with a given server
36+
server := servers.Get(computeClient, serverID)
37+
serverDetails, err := server.Extract()
38+
if err != nil {
39+
slog.Error("Failed to get server details", "ID", serverID)
40+
return "", err
41+
}
42+
43+
return serverDetails.UserID, nil
44+
}
45+
46+
func getUsername(identityClient *gophercloud.ServiceClient, userID string) (string, error) {
47+
// Get the username associated with a given user ID
48+
user := users.Get(identityClient, userID)
49+
userDetails, err := user.Extract()
50+
if err != nil {
51+
slog.Error("Failed to get user details", "ID", userID)
52+
return "", err
53+
}
54+
return userDetails.Name, nil
55+
56+
}
57+
58+
func main() {
59+
http.HandleFunc("/getusername", getUserHandler)
60+
http.ListenAndServe("0.0.0.0:9999", nil)
61+
}
62+
63+
func getUserHandler(w http.ResponseWriter, r *http.Request) {
64+
65+
ID := r.URL.Query().Get("serverID")
66+
userID, err := getServerUserID(computeClient, ID)
67+
if err != nil {
68+
http.Error(w, err.Error(), http.StatusInternalServerError)
69+
return
70+
}
71+
name, err := getUsername(identityClient, userID)
72+
if err != nil {
73+
http.Error(w, err.Error(), http.StatusInternalServerError)
74+
return
75+
}
76+
fmt.Fprintf(w, "%s", name)
77+
}

stfc-username-service/version.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1.0.0

0 commit comments

Comments
 (0)