secrets: gomodules.xyz/secrets/types Index | Examples | Files

package types

import "gomodules.xyz/secrets/types"

Index

Examples

Package Files

secure_string.go url_generator.go

func Config Uses

func Config(fn URLGenerator)

Config should be called once at the start of a program to configure URLGenerator

type SecureString Uses

type SecureString struct {
    URL    string `json:"u"`
    Data   string `json:"-"` // Value
    Cipher []byte `json:"c"`
}

Code:

package main

import (
    "fmt"

    "gomodules.xyz/secrets/types"

    "github.com/go-xorm/xorm"
    _ "github.com/lib/pq"
    _ "gocloud.dev/secrets/gcpkms"
)

// User describes a user
type Credential struct {
    Id   int64
    Name string
    Data types.SecureString `xorm:"text"`
}

func main() {
    Orm, err := newPGEngine("postgres", "postgres", "127.0.0.1", 5432, "postgres")
    if err != nil {
        fmt.Println(err)
        return
    }

    err = Orm.CreateTables(&Credential{})
    if err != nil {
        fmt.Println(err)
        return
    }

    url := fmt.Sprintf("gcpkms://projects/%v/locations/%v/keyRings/%v/cryptoKeys/%v", "ackube", "global", "gitea", "gitea-key")
    fmt.Println(url)
    _, err = Orm.Insert(&Credential{1, "test", types.SecureString{
        URL:  url,
        Data: "this is a test credential",
    }})
    if err != nil {
        fmt.Println(err)
        return
    }

    creds := make([]Credential, 0)
    err = Orm.Find(&creds)
    if err != nil {
        fmt.Println(err)
        return
    }

    fmt.Println(creds)
}

// Connects to any databse using provided credentials
func newPGEngine(user, password, host string, port int64, dbName string) (*xorm.Engine, error) {
    cnnstr := fmt.Sprintf("user=%v password=%v host=%v port=%v dbname=%v sslmode=disable",
        user, password, host, port, dbName)
    engine, err := xorm.NewEngine("postgres", cnnstr)
    if err != nil {
        return nil, err
    }
    // engine.ShowSQL(system.Env() == system.DevEnvironment)
    engine.ShowSQL(true)
    return engine, nil
}

func (*SecureString) FromDB Uses

func (s *SecureString) FromDB(data []byte) error

func (*SecureString) String Uses

func (s *SecureString) String() string

func (*SecureString) ToDB Uses

func (s *SecureString) ToDB() ([]byte, error)

type URLGenerator Uses

type URLGenerator func() string

Package types imports 5 packages (graph) and is imported by 3 packages. Updated 2019-09-30. Refresh now. Tools for package owners.