This repository has been archived on 2022-07-28. You can view files and clone it, but cannot push or open issues or pull requests.
browser_csr_generation/migrations/00010_go.go
2022-07-28 18:46:14 +02:00

89 lines
2.2 KiB
Go

package migrations
import (
"crypto/md5"
"crypto/rand"
"crypto/sha1"
"database/sql"
"errors"
"fmt"
"time"
"github.com/pressly/goose"
log "github.com/sirupsen/logrus"
)
func init() {
goose.AddMigration(Up20201214193523, Down20201214193523)
}
func Up20201214193523(tx *sql.Tx) error {
// This code is executed when the migration is applied.
var row *sql.Row
row = tx.QueryRow("SELECT COUNT(*) FROM users WHERE admin=1")
var data int
if err := row.Scan(&data); err != nil {
return err
}
log.Infof("%d admins found\n", data)
if data == 0 {
location, err := time.LoadLocation("Europe/Berlin")
if err != nil {
return err
}
dob, err := time.ParseInLocation(
"2006-01-02",
"1977-08-08",
location,
)
if err != nil {
return err
}
row = tx.QueryRow("SELECT id FROM countries WHERE name='Germany'")
var ccid int
if err := row.Scan(&ccid); err != nil {
return err
}
regid := 0
locid := 0
random64Bytes := make([]byte, 64)
_, err = rand.Read(random64Bytes)
if err != nil {
return err
}
result, err := tx.Exec(`INSERT INTO users (email, password, fname, mname,
lname, suffix, dob, verified, ccid,
regid, locid, listme, codesign, 1024bit, contactinfo, admin, orgadmin,
ttpadmin, adadmin, board, tverify, locadmin, language,
Q1, Q2, Q3, Q4, Q5,
A1, A2, A3, A4, A5,
created, modified, deleted, locked, uniqueID, otphash,
otppin, assurer, assurer_blocked, lastLoginAttempt)
VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, ?, ?, 0, 1, 0, ?, 1, 0, 0, 0, 0, 0, 0, ?, '', '', '', '', '', '', '', '', '', '', ?,
?, NULL, 0, SHA1(CONCAT(NOW(), ?)), '', 0, 0, 0, NULL)`,
"jandd@cacert.org",
fmt.Sprintf("%x", sha1.Sum([]byte("abcdefghijklmn")),
"Jan", "", "Dittberner", "", dob, ccid, regid, locid, "Somewhere over the rainbow",
time.Now(), time.Now(), fmt.Sprintf("%x", md5.Sum(random64Bytes))))
if err != nil {
return err
}
lastId, err := result.LastInsertId()
if err != nil {
return err
}
log.Infof("new user id is %d", lastId)
}
return errors.New("TODO: implement")
}
func Down20201214193523(tx *sql.Tx) error {
// This code is executed when the migration is rolled back.
return nil
}