Introducing new PKI management
This commit is contained in:
parent
69640506d0
commit
0259ae8f94
19 changed files with 857 additions and 53 deletions
|
|
@ -1,4 +1,105 @@
|
|||
package fic
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Certificate struct {
|
||||
Id int64 `json:"id,string"`
|
||||
Creation time.Time `json:"creation"`
|
||||
Password string `json:"password"`
|
||||
IdTeam *int64 `json:"id_team"`
|
||||
Revoked *time.Time `json:"revoked"`
|
||||
}
|
||||
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func GetCertificate(serial int64) (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)
|
||||
return
|
||||
}
|
||||
|
||||
func ExistingCertSerial(serial int64) (bool) {
|
||||
c, _ := GetCertificate(serial)
|
||||
return c.Id > 0
|
||||
}
|
||||
|
||||
func RegisterCertificate(serial int64, password string) (Certificate, error) {
|
||||
now := time.Now()
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Certificate) Revoke() (int64, error) {
|
||||
now := time.Now()
|
||||
c.Revoked = &now
|
||||
return c.Update()
|
||||
}
|
||||
|
||||
func (c Certificate) Delete() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM certificates WHERE id_cert = ?", c.Id); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
||||
func ClearCertificates() (int64, error) {
|
||||
if res, err := DBExec("DELETE FROM certificates"); err != nil {
|
||||
return 0, err
|
||||
} else if nb, err := res.RowsAffected(); err != nil {
|
||||
return 0, err
|
||||
} else {
|
||||
return nb, err
|
||||
}
|
||||
}
|
||||
|
|
|
|||
12
libfic/db.go
12
libfic/db.go
|
|
@ -79,6 +79,18 @@ CREATE TABLE IF NOT EXISTS teams(
|
|||
name VARCHAR(255) NOT NULL,
|
||||
color INTEGER NOT NULL
|
||||
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := db.Exec(`
|
||||
CREATE TABLE IF NOT EXISTS certificates(
|
||||
id_cert BIGINT 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)
|
||||
) DEFAULT CHARACTER SET = utf8 COLLATE = utf8_bin;
|
||||
`); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ func GetTeams() ([]Team, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func GetTeam(id int) (Team, error) {
|
||||
func GetTeam(id int64) (Team, error) {
|
||||
var t Team
|
||||
if err := DBQueryRow("SELECT id_team, name, color FROM teams WHERE id_team = ?", id).Scan(&t.Id, &t.Name, &t.Color); err != nil {
|
||||
return t, err
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ func NewClaim(subject string, team *Team, assignee *ClaimAssignee, priority stri
|
|||
func (c Claim) GetTeam() (*Team, error) {
|
||||
if c.IdTeam == nil {
|
||||
return nil, nil
|
||||
} else if t, err := GetTeam(int(*c.IdTeam)); err != nil {
|
||||
} else if t, err := GetTeam(*c.IdTeam); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return &t, nil
|
||||
|
|
|
|||
Reference in a new issue