Skip to content

Commit

Permalink
add go.mod on common and pwlib
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommi2Day committed Feb 9, 2023
1 parent 7a19f61 commit 15a3a37
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 45 deletions.
8 changes: 8 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
go-version-file: "go.mod"
cache: true
check-latest: true
- name: Go vendor
run: for d in common pwlib .; do cd $d; go mod tidy; go mod vendor; cd -; done
- name: Install dependencies
run: go get .
- name: Build
Expand All @@ -30,4 +32,10 @@ jobs:
go-version-file: "go.mod"
cache: true
check-latest: true
- name: Go mod tidy
run: go mod tidy
- name: Go Vendor
run: go mod vendor
- name: Install dependencies
run: go get .
- uses: "golangci/[email protected]"
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# My Go Library


## [v1.2.0 - 2023-02-09]
### New
- add encryption method option to config for go and openssl
- add seperate go.mod on packages

## [v1.1.0 - 2023-02-07]
initial load
### New
Expand Down
7 changes: 7 additions & 0 deletions common/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module github.com/tommi2day/gomodules/common

go 1.19

require github.com/sirupsen/logrus v1.9.0

require golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
15 changes: 15 additions & 0 deletions common/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/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 h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ require (
github.com/golangci/golangci-lint v1.51.1
github.com/jstemmer/go-junit-report/v2 v2.0.0
github.com/mgechev/revive v1.2.5
github.com/sirupsen/logrus v1.9.0
github.com/stretchr/testify v1.8.1
github.com/tommi2day/gomodules/common v0.0.0-20230209110442-da975cc23502
github.com/tommi2day/gomodules/pwlib v0.0.0-20230209110442-da975cc23502
github.com/wadey/gocovmerge v0.0.0-20160331181800-b5bfa59ec0ad
golang.org/x/tools v0.5.0
)
Expand Down Expand Up @@ -139,6 +140,7 @@ require (
github.com/sashamelentyev/usestdlibvars v1.21.1 // indirect
github.com/securego/gosec/v2 v2.14.0 // indirect
github.com/shazow/go-diff v0.0.0-20160112020656-b6b7b6733b8c // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/sivchari/containedctx v1.0.2 // indirect
github.com/sivchari/nosnakecase v1.7.0 // indirect
github.com/sivchari/tenv v1.7.1 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,10 @@ github.com/timonwong/loggercheck v0.9.3 h1:ecACo9fNiHxX4/Bc02rW2+kaJIAMAes7qJ7JK
github.com/timonwong/loggercheck v0.9.3/go.mod h1:wUqnk9yAOIKtGA39l1KLE9Iz0QiTocu/YZoOf+OzFdw=
github.com/tomarrell/wrapcheck/v2 v2.8.0 h1:qDzbir0xmoE+aNxGCPrn+rUSxAX+nG6vREgbbXAR81I=
github.com/tomarrell/wrapcheck/v2 v2.8.0/go.mod h1:ao7l5p0aOlUNJKI0qVwB4Yjlqutd0IvAB9Rdwyilxvg=
github.com/tommi2day/gomodules/common v0.0.0-20230209110442-da975cc23502 h1:GkcWkplEjtlLQSA/OxvW/LGp3Ou6BptuS5MWkz/SGXA=
github.com/tommi2day/gomodules/common v0.0.0-20230209110442-da975cc23502/go.mod h1:tBmaQ8Phsl/Hug9TNulQwu7OFV5ZraFAycSEbhs84aE=
github.com/tommi2day/gomodules/pwlib v0.0.0-20230209110442-da975cc23502 h1:mhdBHdQk2cc6qjRcYS24UgZu2r+mrv6t33jtk49MVpY=
github.com/tommi2day/gomodules/pwlib v0.0.0-20230209110442-da975cc23502/go.mod h1:15gSxcqVedMAIofvAEvN3oChVmonaitz94Rke5HE5sw=
github.com/tommy-muehle/go-mnd/v2 v2.5.1 h1:NowYhSdyE/1zwK9QCLeRb6USWdoif80Ie+v+yU8u1Zw=
github.com/tommy-muehle/go-mnd/v2 v2.5.1/go.mod h1:WsUAkMJMYww6l/ufffCD3m+P7LEvr8TnZn9lwVDlgzw=
github.com/ultraware/funlen v0.0.3 h1:5ylVWm8wsNwH5aWo9438pwvsK0QiqVuUrt9bn7S/iLA=
Expand Down
5 changes: 0 additions & 5 deletions pwlib/get_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
log "github.com/sirupsen/logrus"
)

const (
typeGO = "go"
typeOpenssl = "openssl"
)

// DecryptFile decripts an rsa protected file
func DecryptFile() (lines []string, err error) {
cryptedfile := PwConfig.CryptedFile
Expand Down
14 changes: 14 additions & 0 deletions pwlib/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module github.com/tommi2day/gomodules/pwlib

go 1.19

require (
github.com/Luzifer/go-openssl/v4 v4.1.0
github.com/sirupsen/logrus v1.9.0
github.com/tommi2day/gomodules/common v0.0.0-20230209110442-da975cc23502
)

require (
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 // indirect
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 // indirect
)
26 changes: 26 additions & 0 deletions pwlib/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/Luzifer/go-openssl/v4 v4.1.0 h1:8qi3Z6f8Aflwub/Cs4FVSmKUEg/lC8GlODbR2TyZ+nM=
github.com/Luzifer/go-openssl/v4 v4.1.0/go.mod h1:3i1T3Pe6eQK19d86WhuQzjLyMwBaNmGmt3ZceWpWVa4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tommi2day/gomodules/common v0.0.0-20230209110442-da975cc23502 h1:GkcWkplEjtlLQSA/OxvW/LGp3Ou6BptuS5MWkz/SGXA=
github.com/tommi2day/gomodules/common v0.0.0-20230209110442-da975cc23502/go.mod h1:tBmaQ8Phsl/Hug9TNulQwu7OFV5ZraFAycSEbhs84aE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9 h1:vEg9joUBmeBcK9iSJftGNf3coIG4HqZElCPehJsfAYM=
golang.org/x/crypto v0.0.0-20200604202706-70a84ac30bf9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8 h1:0A+M6Uqn+Eje4kHMK80dtF3JCXC4ykBgQG4Fe06QRhQ=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
3 changes: 0 additions & 3 deletions pwlib/openssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ import (
log "github.com/sirupsen/logrus"
)

// SSLDigest variable helds common digist algor
var SSLDigest = openssl.BytesToKeySHA256

// PrivateDecryptFileSSL Decrypt a file with private key with openssl API
func PrivateDecryptFileSSL(cryptedFile string, privateKeyFile string, keyPass string, sessionPassFile string) (content string, err error) {
log.Debugf("decrypt %s with private key %s in OpenSSL format", cryptedFile, privateKeyFile)
Expand Down
31 changes: 26 additions & 5 deletions pwlib/pwlib_config.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package pwlib

import (
"github.com/Luzifer/go-openssl/v4"
"os"

log "github.com/sirupsen/logrus"
)

const (
defaultRsaKeySize = 2048
defaultMethod = "go"
typeGO = "go"
typeOpenssl = "openssl"
defaultMethod = typeGO
)

// PassConfig Type for encryption configuration
Expand All @@ -24,18 +27,23 @@ type PassConfig struct {
SessionPassFile string
Method string
KeySize int
SSLDigest openssl.CredsGenerator
}

var label = []byte("")

// PwConfig Encryption configuration
var PwConfig PassConfig

// SSLDigest variable helds common digist algor
var SSLDigest = openssl.BytesToKeySHA256

// SetConfig set encryption configuration
func SetConfig(appname string, datadir string, keydir string, keypass string) {
func SetConfig(appname string, datadir string, keydir string, keypass string, method string) {
log.Debug("SetConfig entered")
log.Debugf("A:%s, P:%s, D:%s, K:%s", appname, keypass, datadir, keydir)
log.Debugf("A:%s, P:%s, D:%s, K:%s, M:%s", appname, keypass, datadir, keydir, method)
// default names
ext := "gp"
wd, _ := os.Getwd()
etc := wd + "/etc"
if datadir == "" {
Expand All @@ -47,7 +55,19 @@ func SetConfig(appname string, datadir string, keydir string, keypass string) {
if keypass == "" {
keypass = appname
}
cryptedfile := datadir + "/" + appname + ".pw"
if method == "" {
method = defaultMethod
}
if method == typeOpenssl {
ext = "pw"
} else if method == typeGO {
ext = "gp"
} else {
log.Warnf("invalid method %s, use method %s", method, defaultMethod)
method = defaultMethod
ext = "gp"
}
cryptedfile := datadir + "/" + appname + "." + ext
privatekeyfile := keydir + "/" + appname + ".pem"
pubkeyfile := keydir + "/" + appname + ".pub"
plainfile := datadir + "/" + appname + ".plain"
Expand All @@ -63,6 +83,7 @@ func SetConfig(appname string, datadir string, keydir string, keypass string) {
PwConfig.PubKeyFile = pubkeyfile
PwConfig.PlainTextFile = plainfile
PwConfig.SessionPassFile = sessionpassfile
PwConfig.Method = defaultMethod
PwConfig.Method = method
PwConfig.KeySize = defaultRsaKeySize
PwConfig.SSLDigest = SSLDigest
}
62 changes: 34 additions & 28 deletions test/get_password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"github.com/stretchr/testify/require"
)

const typeGO = "go"
const typeOpenssl = "openssl"
const plain = `
# Testfile
!default:defuser2:failure
Expand All @@ -23,35 +25,39 @@ testdp:testuser:xxx:yyy

func TestCrypt(t *testing.T) {
// prepare
app := "test_encrypt"
pwlib.SetConfig(app, TestData, TestData, app)
methods := []string{typeGO, typeOpenssl}
for _, m := range methods {
app := "test_encrypt_" + m
pwlib.SetConfig(app, TestData, TestData, app, m)

err := os.Chdir(TestDir)
require.NoErrorf(t, err, "ChDir failed")
filename := pwlib.PwConfig.PlainTextFile
_ = os.Remove(filename)
//nolint gosec
err = os.WriteFile(filename, []byte(plain), 0644)
require.NoErrorf(t, err, "Create testdata failed")
_, _, err = pwlib.GenRsaKey(pwlib.PwConfig.PubKeyFile, pwlib.PwConfig.PrivateKeyFile, pwlib.PwConfig.KeyPass)
require.NoErrorf(t, err, "Prepare Key failed:%s", err)
err := os.Chdir(TestDir)
require.NoErrorf(t, err, "ChDir failed")
filename := pwlib.PwConfig.PlainTextFile
_ = os.Remove(filename)
//nolint gosec
err = os.WriteFile(filename, []byte(plain), 0644)
require.NoErrorf(t, err, "Create testdata failed")
_, _, err = pwlib.GenRsaKey(pwlib.PwConfig.PubKeyFile, pwlib.PwConfig.PrivateKeyFile, pwlib.PwConfig.KeyPass)
require.NoErrorf(t, err, "Prepare Key failed:%s", err)

// run
t.Run("default Encrypt File method "+m, func(t *testing.T) {
err := pwlib.EncryptFile()
assert.NoErrorf(t, err, "Encryption failed: %s", err)
assert.FileExists(t, pwlib.PwConfig.CryptedFile)
})
t.Run("default Decrypt File method "+m, func(t *testing.T) {
plain, err := common.ReadFileByLine(pwlib.PwConfig.PlainTextFile)
require.NoErrorf(t, err, "PlainTextfile %s not readable:%s", err)
expected := len(plain)
content, err := pwlib.DecryptFile()
assert.NoErrorf(t, err, "Decryption failed: %s", err)
assert.NotEmpty(t, content)
actual := len(content)
assert.Equalf(t, expected, actual, "Lines misamtch exp:%d,act:%d", expected, actual)
})
}

// run
t.Run("default Encrypt File", func(t *testing.T) {
err := pwlib.EncryptFile()
assert.NoErrorf(t, err, "Encryption failed: %s", err)
assert.FileExists(t, pwlib.PwConfig.CryptedFile)
})
t.Run("default Decrypt File", func(t *testing.T) {
plain, err := common.ReadFileByLine(pwlib.PwConfig.PlainTextFile)
require.NoErrorf(t, err, "PlainTextfile %s not readable:%s", err)
expected := len(plain)
content, err := pwlib.DecryptFile()
assert.NoErrorf(t, err, "Decryption failed: %s", err)
assert.NotEmpty(t, content)
actual := len(content)
assert.Equalf(t, expected, actual, "Lines misamtch exp:%d,act:%d", expected, actual)
})
}
func TestGetPassword(t *testing.T) {
// prepare
Expand All @@ -63,7 +69,7 @@ func TestGetPassword(t *testing.T) {
hasError bool
}
app := "test_get_pass"
pwlib.SetConfig(app, TestData, TestData, app)
pwlib.SetConfig(app, TestData, TestData, app, typeGO)
err := os.Chdir(TestDir)
require.NoErrorf(t, err, "ChDir failed")
filename := pwlib.PwConfig.PlainTextFile
Expand Down
6 changes: 3 additions & 3 deletions test/openssl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestEncryptToDecrypt(t *testing.T) {
func TestPublicEncryptString(t *testing.T) {
app := "test_encrypt_String"
testdata := TestDir + "/testdata"
pwlib.SetConfig(app, testdata, testdata, "Test")
pwlib.SetConfig(app, testdata, testdata, "Test", typeGO)

err := os.Chdir(TestDir)
require.NoErrorf(t, err, "ChDir failed")
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestOpensslCompString(t *testing.T) {
testdata := TestDir + "/testdata"

// set env
pwlib.SetConfig(app, testdata, testdata, "Test")
pwlib.SetConfig(app, testdata, testdata, "Test", typeOpenssl)
err := os.Chdir(TestDir)
require.NoErrorf(t, err, "ChDir failed")

Expand Down Expand Up @@ -176,7 +176,7 @@ func TestOpensslFile(t *testing.T) {
app := "test_openssl_file"
testdata := TestDir + "/testdata"
// set env
pwlib.SetConfig(app, testdata, testdata, app)
pwlib.SetConfig(app, testdata, testdata, app, typeOpenssl)
err := os.Chdir(TestDir)
require.NoErrorf(t, err, "ChDir failed")
filename := pwlib.PwConfig.PlainTextFile
Expand Down

0 comments on commit 15a3a37

Please sign in to comment.