types

package
Version: v0.2.6 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: Apache-2.0 Imports: 5 Imported by: 1

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Config

func Config(fn URLGenerator)

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

Types

type SecureString

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

import (
	"fmt"

	"gomodules.xyz/secrets/types"

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

// 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
}
Output:

func (*SecureString) FromDB

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

func (*SecureString) String

func (s *SecureString) String() string

func (*SecureString) ToDB

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

type URLGenerator

type URLGenerator func() string

Jump to

Keyboard shortcuts

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