Skip to content

Commit

Permalink
replace os.WriteFile and os.ReadFile
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommi2Day committed Aug 18, 2024
1 parent 0d9afdc commit dd0460a
Show file tree
Hide file tree
Showing 18 changed files with 77 additions and 129 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# Go Library

## [v1.14.6 - 2024-08-18]
## [v1.14.7 - 2024-08-18]
### Changed
- netlib,dblib: change docker network range
- use common.WriteStringToFile/ReadFileToString instead of os.WriteFile and os.ReadFile
- update dependencies
### Fixed
- fix new linter issues
Expand Down
8 changes: 4 additions & 4 deletions common/file_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func TestFileHelper(t *testing.T) {
err := os.Chdir(test.TestDir)
require.NoErrorf(t, err, "ChDir failed")
_ = os.Remove(filename)
//nolint gosec
err = os.WriteFile(filename, []byte(data), 0644)

err = WriteStringToFile(filename, data)
require.NoErrorf(t, err, "Create testdata failed")

// run
Expand Down Expand Up @@ -67,8 +67,8 @@ func TestFileHelper(t *testing.T) {
err := os.Chdir(test.TestDir)
require.NoErrorf(t, err, "ChDir failed")
_ = os.Remove(filename)
//nolint gosec
err = os.WriteFile(filename, []byte(data), 0644)

err = WriteStringToFile(filename, data)
require.NoErrorf(t, err, "Create testdata failed")
lines := strings.Split(data, "\n")

Expand Down
2 changes: 1 addition & 1 deletion common/git_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestGit(t *testing.T) {
})
t.Run("TestNonGit ERROR", func(t *testing.T) {
filename := path.Join(test.TestData, "testgit.txt")
err := os.WriteFile(filename, []byte("test"), 0600)
err := WriteStringToFile(filename, "test")
require.NoErrorf(t, err, "WriteFile failed")
gitDir, err := GetGitRootDir(filename)
assert.NoErrorf(t, err, "GetGitRootDir should not fail: %s", err)
Expand Down
4 changes: 1 addition & 3 deletions dblib/oracle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ func TestWithOracle(t *testing.T) {
test.InitTestDirs()
tnsAdmin = test.TestData
filename := tnsAdmin + "/connect.ora"
//_ = os.Chdir(tnsAdmin)
//nolint gosec
_ = os.WriteFile(filename, []byte(connectora), 0644)
_ = common.WriteStringToFile(filename, connectora)

t.Logf("load from %s", filename)
domain, _ := ReadSqlnetOra(tnsAdmin)
Expand Down
4 changes: 3 additions & 1 deletion dblib/tns_dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"os"
"testing"

"github.com/tommi2day/gomodules/common"

"github.com/tommi2day/gomodules/netlib"

"github.com/stretchr/testify/assert"
Expand All @@ -28,7 +30,7 @@ func TestRACInfo(t *testing.T) {
require.NoErrorf(t, err, "ChDir failed")

//nolint gosec
err = os.WriteFile(tnsAdmin+"/racinfo.ini", []byte(racinfoini), 0644)
err = common.WriteStringToFile(tnsAdmin+"/racinfo.ini", racinfoini)
require.NoErrorf(t, err, "Create test racinfo.ini failed")

if os.Getenv("SKIP_DNS") != "" {
Expand Down
7 changes: 3 additions & 4 deletions dblib/tns_ldap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ func TestOracleLdap(t *testing.T) {
ldapAdmin := test.TestData

t.Run("Parse wrong ldap.ora", func(t *testing.T) {
//nolint gosec
err = os.WriteFile(ldapAdmin+"/ldap.ora", []byte(ldaporaFail), 0644)
err = common.WriteStringToFile(ldapAdmin+"/ldap.ora", ldaporaFail)
require.NoErrorf(t, err, "Create test ldap.ora failed")
_, ldapservers := ReadLdapOra(ldapAdmin)
e := 1
Expand All @@ -73,8 +72,8 @@ func TestOracleLdap(t *testing.T) {
assert.Equal(t, expected, actual, "ldap entry 1 not match")
}
})
//nolint gosec
err = os.WriteFile(ldapAdmin+"/ldap.ora", []byte(ldaporaOK), 0644)

err = common.WriteStringToFile(ldapAdmin+"/ldap.ora", ldaporaOK)
require.NoErrorf(t, err, "Create test ldap.ora failed")
t.Run("Parse ldap.ora", func(t *testing.T) {
oraclecontext, ldapservers := ReadLdapOra(ldapAdmin)
Expand Down
12 changes: 5 additions & 7 deletions dblib/tns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"strings"
"testing"

"github.com/tommi2day/gomodules/common"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tommi2day/gomodules/test"
Expand Down Expand Up @@ -86,15 +88,11 @@ func TestParseTns(t *testing.T) {
test.InitTestDirs()
err = os.Chdir(test.TestDir)
require.NoErrorf(t, err, "ChDir failed")

//nolint gosec
err = os.WriteFile(tnsAdmin+"/sqlnet.ora", []byte(sqlnetcontent), 0644)
err = common.WriteStringToFile(tnsAdmin+"/sqlnet.ora", sqlnetcontent)
require.NoErrorf(t, err, "Create test sqlnet.ora failed")
//nolint gosec
err = os.WriteFile(tnsAdmin+"/tnsnames.ora", []byte(tnsnamesora), 0644)
err = common.WriteStringToFile(tnsAdmin+"/tnsnames.ora", tnsnamesora)
require.NoErrorf(t, err, "Create test tnsnames.ora failed")
//nolint gosec
err = os.WriteFile(tnsAdmin+"/ifile.ora", []byte(ifileora), 0644)
err = common.WriteStringToFile(tnsAdmin+"/ifile.ora", ifileora)
require.NoErrorf(t, err, "Create test ifile.ora failed")
t.Run("Check TNS_ADMIN", func(t *testing.T) {
var actual string
Expand Down
19 changes: 9 additions & 10 deletions pwlib/encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@ package pwlib

import (
"encoding/base64"
"os"

"github.com/tommi2day/gomodules/common"

log "github.com/sirupsen/logrus"
)

// EncodeFile encodes a file using base64
func EncodeFile(plainFile string, targetFile string) (err error) {
var plaindata []byte
plainData := ""
log.Debugf("Encrypt %s with B64 to %s", plainFile, targetFile)
//nolint gosec
plaindata, err = os.ReadFile(plainFile)
plainData, err = common.ReadFileToString(plainFile)
if err != nil {
log.Debugf("Cannot read plaintext file %s:%s", plainFile, err)
return
}
b64 := base64.StdEncoding.EncodeToString(plaindata)
//nolint gosec
err = os.WriteFile(targetFile, []byte(b64), 0644)
b64 := base64.StdEncoding.EncodeToString([]byte(plainData))
err = common.WriteStringToFile(targetFile, b64)
if err != nil {
log.Debugf("Cannot write: %s", err.Error())
return
Expand All @@ -29,15 +29,14 @@ func EncodeFile(plainFile string, targetFile string) (err error) {

// DecodeFile decodes a file using base64
func DecodeFile(cryptedfile string) (content []byte, err error) {
var data []byte
data := ""
log.Debugf("decrypt b64 %s", cryptedfile)
//nolint gosec
data, err = os.ReadFile(cryptedfile)
data, err = common.ReadFileToString(cryptedfile)
if err != nil {
log.Debugf("Cannot Read file '%s': %s", cryptedfile, err)
return
}
bindata, err := base64.StdEncoding.DecodeString(string(data))
bindata, err := base64.StdEncoding.DecodeString(data)
if err != nil {
log.Debugf("decode base64 for %s failed: %s", cryptedfile, err)
return
Expand Down
4 changes: 1 addition & 3 deletions pwlib/get_password.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ func (pc *PassConfig) DecryptFile() (lines []string, err error) {
data, err = DecodeFile(cryptedfile)
content = string(data)
case typePlain:
//nolint gosec
data, err = os.ReadFile(cryptedfile)
content = string(data)
content, err = common.ReadFileToString(cryptedfile)
case typeVault:
content, err = GetVaultSecret(cryptedfile, "", "")
case typeGPG:
Expand Down
6 changes: 2 additions & 4 deletions pwlib/get_password_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func TestCrypt(t *testing.T) {
pc := NewConfig(app, dataDir, keyDir, app, m)
filename := pc.PlainTextFile
_ = os.Remove(filename)
//nolint gosec
err = os.WriteFile(filename, []byte(plain), 0644)
err = common.WriteStringToFile(filename, plain)
require.NoErrorf(t, err, "Create testdata failed")

// genkey or use existing for GPG
Expand Down Expand Up @@ -93,8 +92,7 @@ func TestGetPassword(t *testing.T) {
require.NoErrorf(t, err, "ChDir failed")
filename := pc.PlainTextFile
_ = os.Remove(filename)
//nolint gosec
err = os.WriteFile(filename, []byte(plain), 0644)
err = common.WriteStringToFile(filename, plain)
require.NoErrorf(t, err, "Create testdata failed")
_, _, err = GenRsaKey(pc.PubKeyFile, pc.PrivateKeyFile, pc.KeyPass)
require.NoErrorf(t, err, "Prepare Key failed:%s", err)
Expand Down
3 changes: 1 addition & 2 deletions pwlib/gpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,7 @@ func GPGEncryptFile(plainFile string, targetFile string, publicKeyFile string) (
if err != nil {
return
}
//nolint gosec
err = os.WriteFile(targetFile, encryptedBytes, 0644)
err = common.WriteStringToFile(targetFile, string(encryptedBytes))
return
}

Expand Down
3 changes: 1 addition & 2 deletions pwlib/gpg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ func TestGPG(t *testing.T) {
}
})
plaintextfile := path.Join(test.TestData, "test.gpg.txt")
//nolint gosec
err = os.WriteFile(plaintextfile, []byte(plain), 0644)
err = common.WriteStringToFile(plaintextfile, plain)
require.NoErrorf(t, err, "Create testdata failed")
cryptedfile := path.Join(test.TestData, "test.gpg.crypt")
t.Run("Encrypt GPG File", func(t *testing.T) {
Expand Down
30 changes: 8 additions & 22 deletions pwlib/kms.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"os"
"strings"

"github.com/tommi2day/gomodules/common"
Expand Down Expand Up @@ -332,30 +331,27 @@ func KMSEncryptFile(plainFile string, targetFile string, keyID string, sessionPa
}

if len(sessionPassFile) > 0 {
//nolint gosec
err = os.WriteFile(sessionPassFile, []byte(crypted), 0644)
err = common.WriteStringToFile(sessionPassFile, crypted)
if err != nil {
log.Errorf("Cannot write session Key file %s:%v", sessionPassFile, err)
}
}

//nolint gosec
plaindata, err := os.ReadFile(plainFile)
plainData, err := common.ReadFileToString(plainFile)
if err != nil {
log.Debugf("Cannot read plaintext file %s:%s", plainFile, err)
return
}

o := openssl.New()
// openssl enc -e -aes-256-cbc -md sha246 -base64 -in $SOURCE -out $TARGET -pass pass:$PASSPHRASE
encrypted, err := o.EncryptBytes(sessionKey, plaindata, SSLDigest)
encrypted, err := o.EncryptBytes(sessionKey, []byte(plainData), SSLDigest)
if err != nil {
log.Errorf("cannot encrypt plaintext file %s:%s", plainFile, err)
return
}
// write crypted output file
//nolint gosec
err = os.WriteFile(targetFile, encrypted, 0644)
err = common.WriteStringToFile(targetFile, string(encrypted))
if err != nil {
log.Errorf("Cannot write: %s", err.Error())
return
Expand All @@ -377,28 +373,18 @@ func KMSDecryptFile(cryptedFile string, keyID string, sessionPassFile string) (c
log.Debug(err)
return
}
//nolint gosec
cryptedData, err := os.ReadFile(cryptedFile)

cryptedData, err := common.ReadFileToString(cryptedFile)
if err != nil {
log.Debugf("cannot Read file '%s': %s", cryptedFile, err)
return
}
encSessionKey := ""

var sp []byte
//nolint gosec
sp, err = os.ReadFile(sessionPassFile)
encSessionKey, err = common.ReadFileToString(sessionPassFile)
if err != nil {
log.Debugf("cannot Read file '%s': %s", sessionPassFile, err)
return
}
encSessionKey = string(sp)

if err != nil {
log.Debugf("Cannot Read file '%s': %s", sessionPassFile, err)
return
}

sessionKey, err := KMSDecryptString(svc, keyID, encSessionKey)
if err != nil {
log.Debugf("decode session key failed:%s", err)
Expand All @@ -409,7 +395,7 @@ func KMSDecryptFile(cryptedFile string, keyID string, sessionPassFile string) (c

// OPENSSL enc -d -aes-256-cbc -md sha256 -base64 -in $SOURCE -pass pass:$SESSIONKEY
o := openssl.New()
decoded, err := o.DecryptBytes(sessionKey, cryptedData, SSLDigest)
decoded, err := o.DecryptBytes(sessionKey, []byte(cryptedData), SSLDigest)
if err != nil {
log.Debugf("Cannot decrypt data from '%s': %s", cryptedFile, err)
return
Expand Down
3 changes: 1 addition & 2 deletions pwlib/kms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ func TestKMS(t *testing.T) {
require.NoErrorf(t, err, "ChDir failed")
filename := pc.PlainTextFile
_ = os.Remove(filename)
//nolint gosec
err = os.WriteFile(filename, []byte(plainfile), 0644)
err = common.WriteStringToFile(filename, plainfile)
require.NoErrorf(t, err, "Create testdata failed")

var kmsClient *kms.Client
Expand Down
Loading

0 comments on commit dd0460a

Please sign in to comment.