Skip to content

Commit 1e3bfa4

Browse files
authored
Merge pull request #6 from oidc-mytoken/dev
0.2.0
2 parents 69b2bb4 + 63964b8 commit 1e3bfa4

File tree

193 files changed

+7718
-3304
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

193 files changed

+7718
-3304
lines changed

.github/workflows/go.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ jobs:
1313
runs-on: ubuntu-latest
1414
steps:
1515

16-
- name: Set up Go 1.14
16+
- name: Set up Go 1.16
1717
uses: actions/setup-go@v2
1818
with:
19-
go-version: ^1.14
19+
go-version: 1.16
2020
id: go
2121

2222
- name: Check out code into the Go module directory

.github/workflows/release.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: goreleaser
2+
on:
3+
push:
4+
tags:
5+
- "v*.*.*"
6+
7+
jobs:
8+
goreleaser:
9+
runs-on: ubuntu-latest
10+
steps:
11+
-
12+
name: Checkout
13+
uses: actions/checkout@v2
14+
with:
15+
fetch-depth: 0
16+
-
17+
name: Set up Go
18+
uses: actions/setup-go@v2
19+
with:
20+
go-version: 1.16
21+
-
22+
name: Run GoReleaser
23+
uses: goreleaser/goreleaser-action@v2
24+
with:
25+
version: latest
26+
args: release --rm-dist
27+
env:
28+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ config/config.yaml
88
IP2LOCATION-LITE-DB1.IPV6.BIN
99
/cmd/test
1010
generateDDL.sh
11+
dist/

.goreleaser.yml

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
project_name: mytoken-server
2+
before:
3+
hooks:
4+
- go mod tidy
5+
builds:
6+
- id: server
7+
main: ./cmd/mytoken-server/main.go
8+
binary: mytoken-server
9+
env:
10+
- CGO_ENABLED=0
11+
goos:
12+
- linux
13+
# - windows
14+
# - darwin
15+
- id: setup
16+
main: ./cmd/mytoken-server/mytoken-setup/setup.go
17+
binary: mytoken-setup
18+
env:
19+
- CGO_ENABLED=0
20+
goos:
21+
- linux
22+
- id: garbage
23+
main: ./cmd/mytoken-server/mytoken-dbGarbageCollector/main.go
24+
binary: mytoken-dbgc
25+
env:
26+
- CGO_ENABLED=0
27+
goos:
28+
- linux
29+
archives:
30+
- replacements:
31+
darwin: Darwin
32+
linux: Linux
33+
windows: Windows
34+
386: i386
35+
amd64: x86_64
36+
nfpms:
37+
- id: server-pkg
38+
package_name: mytoken-server
39+
builds:
40+
- server
41+
homepage: https://mytoken-doc.data.kit.edu/server/intro
42+
maintainer: Gabriel Zachmann <[email protected]>
43+
description: Mytoken is a central web service with the goal to easily obtain OpenID Connect access tokens across devices.
44+
license: MIT
45+
formats:
46+
# - apk
47+
- deb
48+
- rpm
49+
release: 1
50+
section: misc
51+
empty_folders:
52+
- /var/log/mytoken
53+
contents:
54+
- src: config/example-config.yaml
55+
dst: /etc/mytoken/example-config.yaml
56+
type: config
57+
- src: config/mytoken.service
58+
dst: /etc/systemd/system/mytoken.service
59+
- id: setup-pkg
60+
package_name: mytoken-server-setup
61+
builds:
62+
- setup
63+
homepage: https://mytoken-doc.data.kit.edu/server/intro
64+
maintainer: Gabriel Zachmann <[email protected]>
65+
description: A setup utility for the mytoken-server
66+
license: MIT
67+
formats:
68+
- deb
69+
- rpm
70+
release: 1
71+
section: misc
72+
- id: garbage-pkg
73+
package_name: mytoken-server-dbgc
74+
builds:
75+
- garbage
76+
homepage: https://mytoken-doc.data.kit.edu/server/intro
77+
maintainer: Gabriel Zachmann <[email protected]>
78+
description: A garbage collector for the mytoken database. It deletes expired entries.
79+
license: MIT
80+
formats:
81+
- deb
82+
- rpm
83+
release: 1
84+
section: misc
85+
checksum:
86+
name_template: 'checksums.txt'
87+
snapshot:
88+
name_template: "{{ .Tag }}-next"
89+
release:
90+
draft: true
91+
name_template: "{{.ProjectName}} {{.Version}}"
92+
changelog:
93+
sort: asc
94+
filters:
95+
exclude:
96+
- '^docs:'
97+
- '^test:'

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
![mytoken logo](https://git.scc.kit.edu/oidc/mytoken/-/raw/master/docs/img/mytoken.png)
22

33
[![License](https://img.shields.io/github/license/oidc-mytoken/server.svg)](https://github.com/oidc-mytoken/server/blob/master/LICENSE)
4+
![GitHub go.mod Go version](https://img.shields.io/github/go-mod/go-version/oidc-mytoken/server)
5+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/oidc-mytoken/server/Go)
46
[![Go Report](https://goreportcard.com/badge/github.com/oidc-mytoken/server)](https://goreportcard.com/report/github.com/oidc-mytoken/server)
7+
[![DeepSource](https://deepsource.io/gh/oidc-mytoken/server.svg/?label=active+issues&show_trend=true)](https://deepsource.io/gh/oidc-mytoken/server/?ref=repository-badge)
58
[![Release date](https://img.shields.io/github/release-date/oidc-mytoken/server.svg)](https://github.com/oidc-mytoken/server/releases/latest)
69
[![Release version](https://img.shields.io/github/release/oidc-mytoken/server.svg)](https://github.com/oidc-mytoken/server/releases/latest)
710

@@ -16,6 +19,8 @@ The power of a super token can be restricted by the user, so he can create exact
1619

1720
The mytoken command line client can be found at [https://github.com/oidc-mytoken/client](https://github.com/oidc-mytoken/client).
1821

19-
Documentation is available at [https://docs-sdm.scc.kit.edu/mytoken](https://docs-sdm.scc.kit.edu/mytoken) (currently no public access).
22+
A go library for interacting with the mytoken server can be found at [https://github.com/oidc-mytoken/lib](https://github.com/oidc-mytoken/lib).
23+
24+
Documentation is available at [https://mytoken-docs.data.kit.edu/](https://mytoken-docs.data.kit.edu/).
2025

2126
A demo instance of mytoken is running at [https://mytoken.data.kit.edu/](https://mytoken.data.kit.edu/).

cmd/mytoken-server/main.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@ func main() {
2525
server.Init()
2626
configurationEndpoint.Init()
2727
authcode.Init()
28-
if err := db.Connect(); err != nil {
29-
log.WithError(err).Fatal()
30-
}
28+
db.Connect()
3129
jws.LoadKey()
3230
httpClient.Init(config.Get().IssuerURL)
3331
geoip.Init()
@@ -56,9 +54,7 @@ func reload() {
5654
config.Load()
5755
loggerUtils.SetOutput()
5856
loggerUtils.MustUpdateAccessLogger()
59-
if err := db.Connect(); err != nil {
60-
log.WithError(err).Fatal()
61-
}
57+
db.Connect()
6258
jws.LoadKey()
6359
geoip.Init()
6460
}
Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"github.com/jmoiron/sqlx"
45
log "github.com/sirupsen/logrus"
56

67
"github.com/oidc-mytoken/server/internal/config"
@@ -11,21 +12,24 @@ import (
1112
func main() {
1213
config.Load()
1314
loggerUtils.Init()
14-
if err := db.Connect(); err != nil {
15-
log.WithError(err).Fatal()
16-
}
15+
db.Connect()
1716
deleteExpiredTransferCodes()
1817
deleteExpiredAuthInfo()
1918
}
2019

21-
func deleteExpiredTransferCodes() {
22-
if _, err := db.DB().Exec(`DELETE FROM ProxyTokens WHERE id = ANY(SELECT id FROM TransferCodesAttributes WHERE expires_at < CURRENT_TIMESTAMP())`); err != nil {
20+
func execSimpleQuery(sql string) {
21+
if err := db.RunWithinTransaction(nil, func(tx *sqlx.Tx) error {
22+
_, err := tx.Exec(sql)
23+
return err
24+
}); err != nil {
2325
log.WithError(err).Error()
2426
}
2527
}
2628

29+
func deleteExpiredTransferCodes() {
30+
execSimpleQuery(`DELETE FROM ProxyTokens WHERE id = ANY(SELECT id FROM TransferCodesAttributes WHERE expires_at < CURRENT_TIMESTAMP())`)
31+
}
32+
2733
func deleteExpiredAuthInfo() {
28-
if _, err := db.DB().Exec(`DELETE FROM AuthInfo WHERE expires_at < CURRENT_TIMESTAMP()`); err != nil {
29-
log.WithError(err).Error()
30-
}
34+
execSimpleQuery(`DELETE FROM AuthInfo WHERE expires_at < CURRENT_TIMESTAMP()`)
3135
}

cmd/mytoken-server/mytoken-setup/setup.go

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
11
package main
22

33
import (
4+
"database/sql"
45
"errors"
56
"fmt"
67
"io/ioutil"
78
"os"
89
"strings"
910

1011
"github.com/Songmu/prompter"
11-
"github.com/jessevdk/go-flags"
12+
flags "github.com/jessevdk/go-flags"
1213
"github.com/jmoiron/sqlx"
1314
log "github.com/sirupsen/logrus"
1415

1516
"github.com/oidc-mytoken/server/internal/config"
16-
"github.com/oidc-mytoken/server/internal/db"
17+
"github.com/oidc-mytoken/server/internal/db/cluster"
1718
"github.com/oidc-mytoken/server/internal/db/dbdefinition"
1819
"github.com/oidc-mytoken/server/internal/jws"
1920
"github.com/oidc-mytoken/server/internal/model"
2021
loggerUtils "github.com/oidc-mytoken/server/internal/utils/logger"
2122
"github.com/oidc-mytoken/server/internal/utils/zipdownload"
22-
model2 "github.com/oidc-mytoken/server/pkg/model"
23-
event "github.com/oidc-mytoken/server/shared/supertoken/event/pkg"
23+
model2 "github.com/oidc-mytoken/server/shared/model"
24+
event "github.com/oidc-mytoken/server/shared/mytoken/event/pkg"
2425
"github.com/oidc-mytoken/server/shared/utils/fileutil"
2526
)
2627

@@ -35,11 +36,19 @@ func main() {
3536
loggerUtils.Init()
3637

3738
parser := flags.NewNamedParser("mytoken", flags.HelpFlag|flags.PassDoubleDash)
38-
parser.AddCommand("signing-key", "Generates a new signing key", "Generates a new signing key according to the properties specified in the config file and stores it.", &genSigningKeyComm)
39-
parser.AddCommand("db", "Setups the database", "Setups the database as needed and specified in the config file.", &createDBComm)
40-
parser.AddCommand("install", "Installs needed dependencies", "", &installComm)
41-
_, err := parser.Parse()
42-
if err != nil {
39+
if _, err := parser.AddCommand("signing-key", "Generates a new signing key", "Generates a new signing key according to the properties specified in the config file and stores it.", &genSigningKeyComm); err != nil {
40+
log.WithError(err).Fatal()
41+
os.Exit(1)
42+
}
43+
if _, err := parser.AddCommand("db", "Setups the database", "Setups the database as needed and specified in the config file.", &createDBComm); err != nil {
44+
log.WithError(err).Fatal()
45+
os.Exit(1)
46+
}
47+
if _, err := parser.AddCommand("install", "Installs needed dependencies", "", &installComm); err != nil {
48+
log.WithError(err).Fatal()
49+
os.Exit(1)
50+
}
51+
if _, err := parser.Parse(); err != nil {
4352
var flagError *flags.Error
4453
if errors.As(err, &flagError) {
4554
if flagError.Type == flags.ErrHelp {
@@ -100,15 +109,15 @@ func (c *commandGenSigningKey) Execute(args []string) error {
100109
// Execute implements the flags.Commander interface
101110
func (c *commandCreateDB) Execute(args []string) error {
102111
password := ""
103-
if c.Password != nil && len(*c.Password) == 0 { // -p specified without argument
112+
if c.Password != nil && *c.Password == "" { // -p specified without argument
104113
password = prompter.Password("Database Password")
105114
}
106-
dsn := fmt.Sprintf("%s:%s@%s(%s)/", c.Username, password, "tcp", config.Get().DB.Host)
107-
if err := db.ConnectDSN(dsn); err != nil {
108-
return err
109-
}
110-
log.WithField("user", c.Username).Debug("Connected to database")
111-
if err := checkDB(); err != nil {
115+
db := cluster.NewFromConfig(config.DBConf{
116+
Hosts: config.Get().DB.Hosts,
117+
User: c.Username,
118+
Password: password,
119+
})
120+
if err := checkDB(db); err != nil {
112121
return err
113122
}
114123
err := db.Transact(func(tx *sqlx.Tx) error {
@@ -121,7 +130,7 @@ func (c *commandCreateDB) Execute(args []string) error {
121130
if err := createTables(tx); err != nil {
122131
return err
123132
}
124-
if err := addPredefinedValues(tx); err != nil {
133+
if err := addPredefinedValues(tx); err != nil { // skipcq RVV-B0005
125134
return err
126135
}
127136
return nil
@@ -160,7 +169,7 @@ func createTables(tx *sqlx.Tx) error {
160169
}
161170
for _, cmd := range dbdefinition.DDL {
162171
cmd = strings.TrimSpace(cmd)
163-
if len(cmd) > 0 && !strings.HasPrefix(cmd, "--") {
172+
if cmd != "" && !strings.HasPrefix(cmd, "--") {
164173
log.Trace(cmd)
165174
if _, err := tx.Exec(cmd); err != nil {
166175
return err
@@ -199,16 +208,21 @@ func createUser(tx *sqlx.Tx) error {
199208
return nil
200209
}
201210

202-
func checkDB() error {
211+
func checkDB(db *cluster.Cluster) error {
203212
log.WithField("database", config.Get().DB.DB).Debug("Check if database already exists")
204-
rows, err := db.DB().Query(`SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=?`, config.Get().DB.DB)
205-
if err != nil {
213+
var rows *sql.Rows
214+
if err := db.Transact(func(tx *sqlx.Tx) error {
215+
var err error
216+
rows, err = tx.Query(`SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME=?`, config.Get().DB.DB)
217+
return err
218+
}); err != nil {
206219
return err
207220
}
208221
defer rows.Close()
209222
if rows.Next() {
210223
if !prompter.YesNo("The database already exists. If we continue all data will be deleted. Do you want to continue?", false) {
211-
os.Exit(1)
224+
_ = rows.Close()
225+
os.Exit(1) // skipcq CRT-D0011
212226
}
213227
}
214228
return nil

0 commit comments

Comments
 (0)