lockset

package module
v0.0.0-...-0200d27 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2022 License: ISC Imports: 9 Imported by: 0

README

go-lockset

A go package to add support for data at rest encryption if you are using the database/sql to access your database.

Installation

In your Golang project, please run:

go get github.com/bartmika/go-lockset

Documentation

All documentation can be found here.

Usage

package main

import (
	// ...

	"github.com/bartmika/go-lockset"
)

func main() {
	// Before you begin, make sure you have an environment variable set `LOCKSET_ENTRY_KEY` or the following.
	SetLocksetEntryKey("sdfdsfedrdsfsdfsdfazfasfasdfsdfa")

    // To encrypt the text "Hello World", wrap it around the `LockString` struct as follows.
	ls := lockset.LockString{Plaintext: "Hello World"}
	var tenantID uint64 = 1

	//
	// Save to the database.
	//

	ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
	defer cancel()

	query := `
    INSERT INTO bulletin_board_items (
        tenant_id, text
    ) VALUES (
        $1, $2
    )`
	stmt, err := r.db.PrepareContext(ctx, query)
	if err != nil {
		panic(err)
	}
	defer stmt.Close()

	_, err = stmt.ExecContext(ctx, tenantID, ls)

	//
	// Get from the database
	//

	var newlst lockset.LockString

	query = `
    SELECT
        text
    WHERE
        tenant_id = $1`
	err = r.db.QueryRowContext(ctx, query, tenant_id).Scan(
		&newlst,
	)
	if err != nil {
		// CASE 1 OF 2: Cannot find record with that email.
		if err == sql.ErrNoRows {
			return // Do nothing...
		}
		// CASE 2 OF 2: All other errors.
		return panic(err)
		}
	}

    // You should see the result!
	fmt.Println(newlst.Plaintext)
}

Contributing

Found a bug? Want a feature to improve your developer experience when dealing with the time package? Please create an issue.

License

Made with ❤️ by Bartlomiej Mika.
The project is licensed under the ISC License.

Resource used:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetLocksetEntryKey

func GetLocksetEntryKey() []byte

GetLocksetEntryKey returns the password used for encryption.

func SetLocksetEntryKey

func SetLocksetEntryKey(k []byte) error

SetLocksetEntryKey sets the password to use for encryption.

Types

type LockString

type LockString struct {
	Plaintext string
}

LockString represents the string value that will get encrypted when the data is stored in the database.

func (*LockString) Scan

func (ls *LockString) Scan(value interface{}) error

Scan implements sql.Scanner interface from the database/sql package and decryptes incoming sql column data.

func (LockString) Value

func (ls LockString) Value() (driver.Value, error)

Value implementing the driver.Valuer interface from the database/sql package and encrypts the outgoing sql column data.

Jump to

Keyboard shortcuts

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