admin/pki: use symlink instead of DB to associate certificate to team

This commit is contained in:
nemunaire 2018-04-13 20:59:03 +02:00 committed by Pierre-Olivier Mercier
commit 156a87abc0
4 changed files with 83 additions and 46 deletions

View file

@ -20,20 +20,19 @@ type Certificate struct {
Id uint64 `json:"id,string"`
Creation time.Time `json:"creation"`
Password string `json:"password"`
IdTeam *int64 `json:"id_team"`
Revoked *time.Time `json:"revoked"`
}
// GetCertificates returns the list of all generated certificates.
func GetCertificates() (certificates []Certificate, err error) {
var rows *sql.Rows
if rows, err = DBQuery("SELECT id_cert, creation, password, id_team, revoked FROM certificates ORDER BY creation"); err == nil {
if rows, err = DBQuery("SELECT id_cert, creation, password, revoked FROM certificates ORDER BY creation"); err == nil {
defer rows.Close()
certificates = make([]Certificate, 0)
for rows.Next() {
var c Certificate
if err = rows.Scan(&c.Id, &c.Creation, &c.Password, &c.IdTeam, &c.Revoked); err != nil {
if err = rows.Scan(&c.Id, &c.Creation, &c.Password, &c.Revoked); err != nil {
return
}
certificates = append(certificates, c)
@ -44,28 +43,9 @@ func GetCertificates() (certificates []Certificate, err error) {
}
// GetTeamCertificates returns all certificates generated for a given Team.
func GetTeamCertificates(team Team) (certificates []Certificate, err error) {
var rows *sql.Rows
if rows, err = DBQuery("SELECT id_cert, creation, password, id_team, revoked FROM certificates WHERE id_team = ? ORDER BY creation", team.Id); err == nil {
defer rows.Close()
certificates = make([]Certificate, 0)
for rows.Next() {
var c Certificate
if err = rows.Scan(&c.Id, &c.Creation, &c.Password, &c.IdTeam, &c.Revoked); err != nil {
return
}
certificates = append(certificates, c)
}
err = rows.Err()
}
return
}
// GetCertificate retrieves a certificate from its serial number.
func GetCertificate(serial uint64) (c Certificate, err error) {
err = DBQueryRow("SELECT id_cert, creation, password, id_team, revoked FROM certificates WHERE id_cert = ?", serial).Scan(&c.Id, &c.Creation, &c.Password, &c.IdTeam, &c.Revoked)
err = DBQueryRow("SELECT id_cert, creation, password, revoked FROM certificates WHERE id_cert = ?", serial).Scan(&c.Id, &c.Creation, &c.Password, &c.Revoked)
return
}
@ -87,13 +67,13 @@ func RegisterCertificate(serial uint64, password string) (Certificate, error) {
if _, err := DBExec("INSERT INTO certificates (id_cert, creation, password) VALUES (?, ?, ?)", serial, now, password); err != nil {
return Certificate{}, err
} else {
return Certificate{serial, now, password, nil, nil}, nil
return Certificate{serial, now, password, nil}, nil
}
}
// Update applies modifications back to the database.
func (c Certificate) Update() (int64, error) {
if res, err := DBExec("UPDATE certificates SET creation = ?, password = ?, id_team = ?, revoked = ? WHERE id_cert = ?", c.Creation, c.Password, c.IdTeam, c.Revoked, c.Id); err != nil {
if res, err := DBExec("UPDATE certificates SET creation = ?, password = ?, revoked = ? WHERE id_cert = ?", c.Creation, c.Password, c.Revoked, c.Id); err != nil {
return 0, err
} else if nb, err := res.RowsAffected(); err != nil {
return 0, err

View file

@ -91,9 +91,7 @@ CREATE TABLE IF NOT EXISTS certificates(
id_cert BIGINT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
creation TIMESTAMP NOT NULL,
password VARCHAR(255) NOT NULL,
id_team INTEGER NULL,
revoked TIMESTAMP NULL,
FOREIGN KEY(id_team) REFERENCES teams(id_team)
revoked TIMESTAMP NULL
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
`); err != nil {
return err