Initial commit

This commit is contained in:
斟酌 鵬兄 2022-08-14 17:05:35 +09:00
commit b133c9e23f
3 changed files with 100 additions and 0 deletions

8
go.mod Executable file
View File

@ -0,0 +1,8 @@
module kstorecred
go 1.17
require (
github.com/danieljoos/wincred v1.1.2 // indirect
golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c // indirect
)

10
go.sum Executable file
View File

@ -0,0 +1,10 @@
github.com/danieljoos/wincred v1.1.2 h1:QLdCxFs1/Yl4zduvBdcHB8goaYk9RARS2SgLLRuAyr0=
github.com/danieljoos/wincred v1.1.2/go.mod h1:GijpziifJoIBfYh+S7BbkdUTU4LfM+QnGqR5Vl2tAx0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c h1:Lyn7+CqXIiC+LOR9aHD6jDK+hPcmAuCfuXztd1v4w1Q=
golang.org/x/sys v0.0.0-20210819135213-f52c844e1c1c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

82
main.go Normal file
View File

@ -0,0 +1,82 @@
package main
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/danieljoos/wincred"
)
const (
CRED_NAME = "RBASH-KSTORE"
SECRET_NAME = "_AUTH_SECRET"
)
func usage(stdfd *os.File) {
_name := filepath.Base(os.Args[0])
_space := strings.Repeat(" ", len(_name))
fmt.Fprintf(stdfd, "Usage: %s get\n", _name)
fmt.Fprintf(stdfd, " %s set passphrase\n", _space)
fmt.Fprintf(stdfd, " %s help\n", _space)
}
func getAuthSecret() (*wincred.GenericCredential, error) {
cred, err := wincred.GetGenericCredential(CRED_NAME)
return cred, err
}
func delAuthSecret() error {
cred, _ := getAuthSecret()
if cred == nil {
return nil
}
return cred.Delete()
}
func setAuthSecret(newSecret string) error {
cred, _ := getAuthSecret()
if cred == nil {
cred = wincred.NewGenericCredential(CRED_NAME)
}
cred.CredentialBlob = []byte(newSecret)
return cred.Write()
}
func main() {
if len(os.Args) < 2 {
usage(os.Stderr)
os.Exit(1)
}
var err error
switch os.Args[1] {
case "get":
cred, _err := getAuthSecret()
if _err == nil {
fmt.Println(string(cred.CredentialBlob))
os.Exit(0)
}
err = _err
case "set":
if len(os.Args[2:]) < 1 {
err = errors.New("passphrase is required")
} else {
err = setAuthSecret(os.Args[2])
}
case "del":
err = delAuthSecret()
case "help":
usage(os.Stdout)
}
if err == nil {
os.Exit(0)
}
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}