db

package
v0.0.0-...-1dec843 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package db provides a simplistic ORM to communicate with an SQL database for storage

Example
db, err := db.NewDatabase("./certs.db")
if err != nil {
	log.Fatalln(err)
}
err = db.CreateCertificateRequest(BananaCSR)
if err != nil {
	log.Fatalln(err)
}
err = db.AddCertificateChainToCertificateRequestByCSR(BananaCSR, BananaCert)
if err != nil {
	log.Fatalln(err)
}
entry, err := db.GetCertificateRequestByCSR(BananaCSR)
if err != nil {
	log.Fatalln(err)
}
if entry.CertificateChain != BananaCert {
	log.Fatalln("Retrieved Certificate doesn't match Stored Certificate")
}
err = db.Close()
if err != nil {
	log.Fatalln(err)
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CertificateMatchesCSR

func CertificateMatchesCSR(cert string, csr string) error

CertificateMatchesCSR makes sure that the given certificate and CSR match. The given CSR and Cert must pass their respective validation functions The given CSR and Cert must share the same public key

func ValidateCertificate

func ValidateCertificate(cert string) error

ValidateCertificate validates the given Cert string to the following:

The string must include 2 or more PEM formatted certificate strings. Each cert must be a valid PEM string, and should be capable of being parsed into type x509 CERTIFICATE Each subsequent certificate in the string should be the issuer of the previous string, which means:

All except the first certificate should have the "is a CA" Basic Constraint. The public key of the certificate should match the public key of the following certificate. The issuer field of the certificate should match the subject field of the following certificate.

func ValidateCertificateRequest

func ValidateCertificateRequest(csr string) error

ValidateCertificateRequest validates the given CSR string to the following: The string must be a valid PEM string, and should be of type CERTIFICATE REQUEST The PEM string should be able to be parsed into a x509 Certificate Request

Types

type CertificateRequest

type CertificateRequest struct {
	ID int `db:"id"`

	CSR              string `db:"csr"`
	CertificateChain string `db:"certificate_chain"`
	Status           string `db:"status"`
}

type Database

type Database struct {
	// contains filtered or unexported fields
}

Database is the object used to communicate with the established repository.

func NewDatabase

func NewDatabase(databasePath string) (*Database, error)

NewDatabase connects to a given table in a given database, stores the connection information and returns an object containing the information. The database path must be a valid file path or ":memory:". The table will be created if it doesn't exist in the format expected by the package.

func (*Database) AddCertificateChainToCertificateRequestByCSR

func (db *Database) AddCertificateChainToCertificateRequestByCSR(csr string, cert string) error

AddCertificateChainToCertificateRequestByCSR adds a new certificate chain to a row for a given CSR string.

func (*Database) AddCertificateChainToCertificateRequestByID

func (db *Database) AddCertificateChainToCertificateRequestByID(id int, cert string) error

AddCertificateChainToCSRbyID adds a new certificate chain to a row for a given row ID.

func (*Database) Close

func (db *Database) Close() error

Close closes the connection to the repository cleanly.

func (*Database) CreateCertificateRequest

func (db *Database) CreateCertificateRequest(csr string) error

CreateCertificateRequest creates a new CSR entry in the repository. The string must be a valid CSR and unique.

func (*Database) CreateUser

func (db *Database) CreateUser(username string, password string, permission int) error

CreateUser creates a new user from a given username, password and permission level. The permission level 1 represents an admin, and a 0 represents a regular user. The password passed in should be in plaintext. This function handles hashing and salting the password before storing it in the database.

func (*Database) DeleteCertificateRequestByCSR

func (db *Database) DeleteCertificateRequestByCSR(csr string) error

DeleteCertificateRequestByCSR removes a CSR from the database alongside the certificate that may have been generated for it.

func (*Database) DeleteCertificateRequestByID

func (db *Database) DeleteCertificateRequestByID(id int) error

DeleteCSRByID removes a CSR from the database alongside the certificate that may have been generated for it.

func (*Database) DeleteUserByID

func (db *Database) DeleteUserByID(id int) error

DeleteUserByID removes a user from the table.

func (*Database) GetCertificateRequestByCSR

func (db *Database) GetCertificateRequestByCSR(csr string) (*CertificateRequest, error)

GetCertificateRequestByCSR gets a given CSR row from the repository using the CSR text.

func (*Database) GetCertificateRequestByID

func (db *Database) GetCertificateRequestByID(id int) (*CertificateRequest, error)

GetCertificateRequestByID gets a CSR row from the repository from a given ID.

func (*Database) GetUserByID

func (db *Database) GetUserByID(id int) (*User, error)

GetUserByID retrieves the name, password and the permission level of a user.

func (*Database) GetUserByUsername

func (db *Database) GetUserByUsername(name string) (*User, error)

GetUserByUsername retrieves the id, password and the permission level of a user.

func (*Database) ListCertificateRequests

func (db *Database) ListCertificateRequests() ([]CertificateRequest, error)

ListCertificateRequests gets every CertificateRequest entry in the table.

func (*Database) ListUsers

func (db *Database) ListUsers() ([]User, error)

ListUsers returns all of the users and their fields available in the database.

func (*Database) NumUsers

func (db *Database) NumUsers() (int, error)

NumUsers returns the number of users in the database.

func (*Database) RejectCertificateRequestByCSR

func (db *Database) RejectCertificateRequestByCSR(csr string) error

RejectCertificateRequestByCSR updates input CSR's row by setting the certificate bundle to "" and moving the row status to "Rejected".

func (*Database) RejectCertificateRequestByID

func (db *Database) RejectCertificateRequestByID(id int) error

RejectCSRbyCSR updates input ID's row by setting the certificate bundle to "" and sets the row status to "Rejected".

func (*Database) RevokeCertificateByCSR

func (db *Database) RevokeCertificateByCSR(csr string) error

RevokeCertificateByCSR updates the input CSR's row by setting the certificate bundle to "" and sets the row status to "Revoked".

func (*Database) UpdateUserPassword

func (db *Database) UpdateUserPassword(id int, password string) error

UpdateUser updates the password of the given user. Just like with CreateUser, this function handles hashing and salting the password before storage.

type NumUsers

type NumUsers struct {
	Count int `db:"count"`
}

type User

type User struct {
	ID int `db:"id"`

	Username       string `db:"username"`
	HashedPassword string `db:"hashed_password"`
	Permissions    int    `db:"permissions"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL