Skip to content

Commit 620b0fa

Browse files
easyCZroboquat
authored andcommitted
[oidc] Setup db
1 parent 98ef630 commit 620b0fa

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package dbtest
6+
7+
import (
8+
"context"
9+
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
10+
"github.com/google/uuid"
11+
"github.com/stretchr/testify/require"
12+
"gorm.io/gorm"
13+
"testing"
14+
"time"
15+
)
16+
17+
func NewOIDCClientConfig(t *testing.T, record db.OIDCClientConfig) db.OIDCClientConfig {
18+
t.Helper()
19+
20+
now := time.Now().UTC().Truncate(time.Millisecond)
21+
result := db.OIDCClientConfig{
22+
ID: uuid.New(),
23+
Issuer: "issuer",
24+
Data: []byte("{}"),
25+
LastModified: now,
26+
}
27+
28+
if record.ID != uuid.Nil {
29+
result.ID = record.ID
30+
}
31+
32+
if record.Issuer != "" {
33+
result.Issuer = record.Issuer
34+
}
35+
36+
if record.Data != nil {
37+
result.Data = record.Data
38+
}
39+
40+
return result
41+
}
42+
43+
func CreateOIDCClientConfigs(t *testing.T, conn *gorm.DB, entries ...db.OIDCClientConfig) []db.OIDCClientConfig {
44+
t.Helper()
45+
46+
var records []db.OIDCClientConfig
47+
var ids []string
48+
for _, entry := range entries {
49+
record := NewOIDCClientConfig(t, entry)
50+
records = append(records, record)
51+
ids = append(ids, record.ID.String())
52+
53+
_, err := db.CreateOIDCCLientConfig(context.Background(), conn, record)
54+
require.NoError(t, err)
55+
}
56+
57+
t.Cleanup(func() {
58+
if len(ids) > 0 {
59+
require.NoError(t, conn.Where(ids).Delete(&db.OIDCClientConfig{}).Error)
60+
}
61+
})
62+
63+
return records
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package db
6+
7+
import (
8+
"context"
9+
"errors"
10+
"fmt"
11+
"github.com/google/uuid"
12+
"gorm.io/datatypes"
13+
"gorm.io/gorm"
14+
"time"
15+
)
16+
17+
type OIDCClientConfig struct {
18+
ID uuid.UUID `gorm:"primary_key;column:id;type:char;size:36;" json:"id"`
19+
20+
Issuer string `gorm:"column:issuer;type:char;size:255;" json:"issuer"`
21+
22+
Data datatypes.JSON `gorm:"column:data;type:text;size:65535" json:"data"`
23+
24+
LastModified time.Time `gorm:"column:_lastModified;type:timestamp;default:CURRENT_TIMESTAMP(6);" json:"_lastModified"`
25+
// deleted is reserved for use by db-sync.
26+
_ bool `gorm:"column:deleted;type:tinyint;default:0;" json:"deleted"`
27+
}
28+
29+
func (c *OIDCClientConfig) TableName() string {
30+
return "d_b_oidc_client_config"
31+
}
32+
33+
func CreateOIDCCLientConfig(ctx context.Context, conn *gorm.DB, cfg OIDCClientConfig) (OIDCClientConfig, error) {
34+
if cfg.ID == uuid.Nil {
35+
return OIDCClientConfig{}, errors.New("OIDC Client Config ID must be set")
36+
}
37+
38+
if cfg.Issuer == "" {
39+
return OIDCClientConfig{}, errors.New("OIDC Client Config issuer must be set")
40+
}
41+
42+
tx := conn.
43+
WithContext(ctx).
44+
Create(&cfg)
45+
if tx.Error != nil {
46+
return OIDCClientConfig{}, fmt.Errorf("failed to create oidc client config: %w", tx.Error)
47+
}
48+
49+
return cfg, nil
50+
}
51+
52+
func GetOIDCClientConfig(ctx context.Context, conn *gorm.DB, id uuid.UUID) (OIDCClientConfig, error) {
53+
var config OIDCClientConfig
54+
55+
if id == uuid.Nil {
56+
return OIDCClientConfig{}, fmt.Errorf("OIDC Client Config ID is a required argument")
57+
}
58+
59+
tx := conn.
60+
WithContext(ctx).
61+
Where("id = ?", id).
62+
Where("deleted = ?", 0).
63+
First(&config)
64+
if tx.Error != nil {
65+
if errors.Is(tx.Error, gorm.ErrRecordNotFound) {
66+
return OIDCClientConfig{}, fmt.Errorf("OIDC Client Config with ID %s does not exist: %w", id, ErrorNotFound)
67+
}
68+
return OIDCClientConfig{}, fmt.Errorf("Failed to retrieve OIDC client config: %v", tx.Error)
69+
}
70+
71+
return config, nil
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2+
// Licensed under the GNU Affero General Public License (AGPL).
3+
// See License.AGPL.txt in the project root for license information.
4+
5+
package db_test
6+
7+
import (
8+
"context"
9+
db "github.com/gitpod-io/gitpod/components/gitpod-db/go"
10+
"github.com/gitpod-io/gitpod/components/gitpod-db/go/dbtest"
11+
"github.com/stretchr/testify/require"
12+
"testing"
13+
)
14+
15+
func TestCreateOIDCClientConfig_Create(t *testing.T) {
16+
conn := dbtest.ConnectForTests(t)
17+
created := dbtest.CreateOIDCClientConfigs(t, conn, db.OIDCClientConfig{})[0]
18+
19+
retrieved, err := db.GetOIDCClientConfig(context.Background(), conn, created.ID)
20+
require.NoError(t, err)
21+
require.Equal(t, created, retrieved)
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Copyright (c) 2022 Gitpod GmbH. All rights reserved.
3+
* Licensed under the GNU Affero General Public License (AGPL).
4+
* See License.AGPL.txt in the project root for license information.
5+
*/
6+
7+
import { MigrationInterface, QueryRunner } from "typeorm";
8+
import { tableExists } from "./helper/helper";
9+
10+
export class CreateOIDCClientConfigTable1670850042413 implements MigrationInterface {
11+
public async up(queryRunner: QueryRunner): Promise<void> {
12+
if (!(await tableExists(queryRunner, "d_b_oidc_client_config"))) {
13+
await queryRunner.query(
14+
"CREATE TABLE IF NOT EXISTS `d_b_oidc_client_config` (`id` varchar(255) NOT NULL, `issuer` varchar(255) NOT NULL, `data` text(65535) NOT NULL, `_lastModified` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), `deleted` tinyint(4) NOT NULL DEFAULT '0', PRIMARY KEY (id))",
15+
);
16+
}
17+
}
18+
19+
public async down(queryRunner: QueryRunner): Promise<void> {
20+
if (await tableExists(queryRunner, "d_b_oidc_client_config")) {
21+
await queryRunner.query("DROP TABLE `d_b_oidc_client_config`");
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)