Skip to content

Commit

Permalink
Use xpasswd instead of passwd pkg (#13)
Browse files Browse the repository at this point in the history
* Bump go version to 1.21 and remove vendor dir

* Remove use of ioutil

* Refactor to use xpasswd instead of passwd
  • Loading branch information
mauromorales authored Jun 6, 2024
1 parent 68348ba commit caa3729
Show file tree
Hide file tree
Showing 697 changed files with 118 additions and 449,364 deletions.
30 changes: 19 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,23 +1,31 @@
module github.com/mudler/entities

go 1.12
go 1.21

require (
github.com/golang/protobuf v1.3.2 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/mauromorales/xpasswd v0.3.0
github.com/olekukonko/tablewriter v0.0.5
github.com/onsi/ginkgo v1.12.0
github.com/onsi/gomega v1.9.0
github.com/onsi/gomega v1.33.0
github.com/phayes/permbits v0.0.0-20190612203442-39d7c581d2ee
github.com/pkg/errors v0.9.1
github.com/spf13/cobra v0.0.6
github.com/spf13/pflag v1.0.5 // indirect
github.com/tredoe/osutil/v2 v2.0.0-rc.16
github.com/willdonnelly/passwd v0.0.0-20141013001024-7935dab3074c
golang.org/x/net v0.0.0-20191209160850-c0dbc17a3553 // indirect
golang.org/x/sys v0.0.0-20200102141924-c96a22e43c9c // indirect
golang.org/x/text v0.3.2 // indirect
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.2.8
)

require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/hpcloud/tail v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/kr/pretty v0.2.0 // indirect
github.com/mattn/go-runewidth v0.0.9 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/fsnotify.v1 v1.4.7 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
65 changes: 26 additions & 39 deletions go.sum

Large diffs are not rendered by default.

10 changes: 4 additions & 6 deletions pkg/entities/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/


package entities

import (
"bufio"
"fmt"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -149,7 +147,7 @@ func (u Group) String() string {

func (u Group) Delete(s string) error {
s = GroupsDefault(s)
input, err := ioutil.ReadFile(s)
input, err := os.ReadFile(s)
if err != nil {
return errors.Wrap(err, "Could not read input file")
}
Expand All @@ -174,7 +172,7 @@ func (u Group) Delete(s string) error {

output := strings.Join(lines, "\n")

err = ioutil.WriteFile(s, []byte(output), os.FileMode(permissions))
err = os.WriteFile(s, []byte(output), os.FileMode(permissions))
if err != nil {
return errors.Wrap(err, "Could not write")
}
Expand Down Expand Up @@ -277,7 +275,7 @@ func (u Group) Apply(s string, safe bool) error {
}

if _, ok := current[u.Name]; ok {
input, err := ioutil.ReadFile(s)
input, err := os.ReadFile(s)
if err != nil {
return errors.Wrap(err, "Could not read input file")
}
Expand Down Expand Up @@ -316,7 +314,7 @@ func (u Group) Apply(s string, safe bool) error {
}
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(s, []byte(output), os.FileMode(permissions))
err = os.WriteFile(s, []byte(output), os.FileMode(permissions))
if err != nil {
return errors.Wrap(err, "Could not write")
}
Expand Down
14 changes: 6 additions & 8 deletions pkg/entities/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/


package entities_test

import (
"fmt"
"io/ioutil"
"os"

. "github.com/mudler/entities/pkg/entities"
Expand All @@ -30,7 +28,7 @@ var _ = Describe("Group", func() {
p := &Parser{}

It("Changes an entry", func() {
tmpFile, err := ioutil.TempFile(os.TempDir(), "pre-")
tmpFile, err := os.CreateTemp(os.TempDir(), "pre-")
if err != nil {
fmt.Println("Cannot create temporary file", err)
}
Expand All @@ -48,7 +46,7 @@ var _ = Describe("Group", func() {
err = entity.Apply(tmpFile.Name(), false)
Expect(err).Should(BeNil())

dat, err := ioutil.ReadFile(tmpFile.Name())
dat, err := os.ReadFile(tmpFile.Name())
Expect(err).Should(BeNil())
Expect(string(dat)).To(Equal(
`nm-openconnect:x:979:
Expand All @@ -63,7 +61,7 @@ ntp:x:123:
})

It("Adds and deletes an entry", func() {
tmpFile, err := ioutil.TempFile(os.TempDir(), "pre-")
tmpFile, err := os.CreateTemp(os.TempDir(), "pre-")
if err != nil {
fmt.Println("Cannot create temporary file", err)
}
Expand All @@ -80,7 +78,7 @@ ntp:x:123:

entity.Apply(tmpFile.Name(), false)

dat, err := ioutil.ReadFile(tmpFile.Name())
dat, err := os.ReadFile(tmpFile.Name())
Expect(err).Should(BeNil())
Expect(string(dat)).To(Equal(
`nm-openconnect:x:979:
Expand All @@ -100,7 +98,7 @@ foo:xx:1:one,two,tree

entity.Apply(tmpFile.Name(), false)

dat, err = ioutil.ReadFile(tmpFile.Name())
dat, err = os.ReadFile(tmpFile.Name())
Expect(err).Should(BeNil())
Expect(string(dat)).To(Equal(
`nm-openconnect:x:979:
Expand All @@ -115,7 +113,7 @@ foo:xx:1:one,two,tree,four
`))

entity.Delete(tmpFile.Name())
dat, err = ioutil.ReadFile(tmpFile.Name())
dat, err = os.ReadFile(tmpFile.Name())
Expect(err).Should(BeNil())
Expect(string(dat)).To(Equal(
`nm-openconnect:x:979:
Expand Down
10 changes: 4 additions & 6 deletions pkg/entities/gshadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@ See the License for the specific language governing permissions and
limitations under the License.
*/


package entities

import (
"bufio"
"bytes"
"io"
"io/ioutil"
"os"
"strconv"
"strings"
Expand Down Expand Up @@ -97,7 +95,7 @@ func (u GShadow) String() string {

func (u GShadow) Delete(s string) error {
s = GShadowDefault(s)
input, err := ioutil.ReadFile(s)
input, err := os.ReadFile(s)
if err != nil {
return errors.Wrap(err, "Could not read input file")
}
Expand All @@ -107,7 +105,7 @@ func (u GShadow) Delete(s string) error {
}
lines := bytes.Replace(input, []byte(u.String()+"\n"), []byte(""), 1)

err = ioutil.WriteFile(s, []byte(lines), os.FileMode(permissions))
err = os.WriteFile(s, []byte(lines), os.FileMode(permissions))
if err != nil {
return errors.Wrap(err, "Could not write")
}
Expand Down Expand Up @@ -169,7 +167,7 @@ func (u GShadow) Apply(s string, safe bool) error {
}

if _, ok := current[u.Name]; ok {
input, err := ioutil.ReadFile(s)
input, err := os.ReadFile(s)
if err != nil {
return errors.Wrap(err, "Could not read input file")
}
Expand All @@ -182,7 +180,7 @@ func (u GShadow) Apply(s string, safe bool) error {
}
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(s, []byte(output), os.FileMode(permissions))
err = os.WriteFile(s, []byte(output), os.FileMode(permissions))
if err != nil {
return errors.Wrap(err, "Could not write")
}
Expand Down
12 changes: 5 additions & 7 deletions pkg/entities/gshadow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/


package entities_test

import (
"fmt"
"io/ioutil"
"os"

. "github.com/mudler/entities/pkg/entities"
Expand All @@ -30,7 +28,7 @@ var _ = Describe("GShadow", func() {
p := &Parser{}

It("Changes an entry", func() {
tmpFile, err := ioutil.TempFile(os.TempDir(), "pre-")
tmpFile, err := os.CreateTemp(os.TempDir(), "pre-")
if err != nil {
fmt.Println("Cannot create temporary file", err)
}
Expand All @@ -48,7 +46,7 @@ var _ = Describe("GShadow", func() {
err = entity.Apply(tmpFile.Name(), false)
Expect(err).Should(BeNil())

dat, err := ioutil.ReadFile(tmpFile.Name())
dat, err := os.ReadFile(tmpFile.Name())
Expect(err).Should(BeNil())
Expect(string(dat)).To(Equal(
`systemd-bus-proxy:!::
Expand All @@ -69,7 +67,7 @@ ldap:!::
})

It("Adds and deletes an entry", func() {
tmpFile, err := ioutil.TempFile(os.TempDir(), "pre-")
tmpFile, err := os.CreateTemp(os.TempDir(), "pre-")
if err != nil {
fmt.Println("Cannot create temporary file", err)
}
Expand All @@ -86,7 +84,7 @@ ldap:!::

entity.Apply(tmpFile.Name(), false)

dat, err := ioutil.ReadFile(tmpFile.Name())
dat, err := os.ReadFile(tmpFile.Name())
Expect(err).Should(BeNil())
Expect(string(dat)).To(Equal(
`systemd-bus-proxy:!::
Expand All @@ -107,7 +105,7 @@ test:!:foo,bar:foo,baz
`))

entity.Delete(tmpFile.Name())
dat, err = ioutil.ReadFile(tmpFile.Name())
dat, err = os.ReadFile(tmpFile.Name())
Expect(err).Should(BeNil())
Expect(string(dat)).To(Equal(
`systemd-bus-proxy:!::
Expand Down
5 changes: 2 additions & 3 deletions pkg/entities/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ See the License for the specific language governing permissions and
limitations under the License.
*/


package entities

import (
"io/ioutil"
"os"

"github.com/pkg/errors"
"gopkg.in/yaml.v2"
Expand Down Expand Up @@ -85,7 +84,7 @@ func (p Parser) ReadEntityFromBytes(yamlFile []byte) (Entity, error) {
return nil, errors.New("Unsupported format")
}
func (p Parser) ReadEntity(entity string) (Entity, error) {
yamlFile, err := ioutil.ReadFile(entity)
yamlFile, err := os.ReadFile(entity)
if err != nil {
return nil, errors.Wrap(err, "Failed while reading entity file")
}
Expand Down
10 changes: 4 additions & 6 deletions pkg/entities/shadow.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,13 @@ See the License for the specific language governing permissions and
limitations under the License.
*/


package entities

import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"math/rand"
"os"
"strconv"
Expand Down Expand Up @@ -168,7 +166,7 @@ func (u Shadow) prepare() Shadow {
// FIXME: Delete can be shared across all of the supported Entities
func (u Shadow) Delete(s string) error {
s = ShadowDefault(s)
input, err := ioutil.ReadFile(s)
input, err := os.ReadFile(s)
if err != nil {
return errors.Wrap(err, "Could not read input file")
}
Expand All @@ -178,7 +176,7 @@ func (u Shadow) Delete(s string) error {
}
lines := bytes.Replace(input, []byte(u.String()+"\n"), []byte(""), 1)

err = ioutil.WriteFile(s, []byte(lines), os.FileMode(permissions))
err = os.WriteFile(s, []byte(lines), os.FileMode(permissions))
if err != nil {
return errors.Wrap(err, "Could not write")
}
Expand Down Expand Up @@ -229,7 +227,7 @@ func (u Shadow) Apply(s string, safe bool) error {
}

if _, ok := current[u.Username]; ok {
input, err := ioutil.ReadFile(s)
input, err := os.ReadFile(s)
if err != nil {
return errors.Wrap(err, "Could not read input file")
}
Expand All @@ -242,7 +240,7 @@ func (u Shadow) Apply(s string, safe bool) error {
}
}
output := strings.Join(lines, "\n")
err = ioutil.WriteFile(s, []byte(output), os.FileMode(permissions))
err = os.WriteFile(s, []byte(output), os.FileMode(permissions))
if err != nil {
return errors.Wrap(err, "Could not write")
}
Expand Down
Loading

0 comments on commit caa3729

Please sign in to comment.