fix: remove ssh keys

This commit is contained in:
Markus Pesch 2020-09-02 13:28:21 +02:00
parent a19d252b73
commit a50da9bbed
Signed by: volker.raschek
GPG Key ID: 852BCC170D81A982
7 changed files with 103 additions and 21 deletions

View File

@ -13,13 +13,12 @@ steps:
- pull_request - pull_request
- tag - tag
# steps: - name: test-unit
# - name: test-unit image: docker.io/volkerraschek/build-image:latest
# image: docker.io/volkerraschek/build-image:latest commands:
# commands: - make test/unit
# - make test/unit when:
# when: event:
# event: - push
# - push - pull_request
# - pull_request - tag
# - tag

3
.gitignore vendored
View File

@ -1 +1,2 @@
bin bin
coverage.txt

View File

@ -64,6 +64,15 @@ PHONY+=clean
clean: clean:
-rm --force --recursive bin/ -rm --force --recursive bin/
# TEST
# ==============================================================================
PHONY+=test/unit
test/unit:
go test -v -race -coverprofile=coverage.txt -covermode=atomic -timeout 600s -count=1 ./...
test/coverage: test/unit
go tool cover -html=coverage.txt
# PHONY # PHONY
# ============================================================================== # ==============================================================================
# Declare the contents of the PHONY variable as phony. We keep that information # Declare the contents of the PHONY variable as phony. We keep that information

1
go.mod
View File

@ -5,4 +5,5 @@ go 1.15
require ( require (
git.cryptic.systems/volker.raschek/go-logger v0.1.0 git.cryptic.systems/volker.raschek/go-logger v0.1.0
github.com/spf13/cobra v1.0.0 github.com/spf13/cobra v1.0.0
github.com/stretchr/testify v1.6.1
) )

4
go.sum
View File

@ -17,6 +17,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= 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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
@ -59,6 +60,7 @@ github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRW
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U= github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic= github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
@ -87,6 +89,7 @@ github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/y
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc=
@ -130,5 +133,6 @@ gopkg.in/resty.v1 v1.12.0/go.mod h1:mDo4pnntr5jdWRML875a/NmxYqAlA73dVijT2AXvQQo=
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
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= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=

30
main.go
View File

@ -127,14 +127,20 @@ Loop:
} }
func removeSSHKeys(sshKeys []*sshKey, removeSSHKeys []*sshKey) []*sshKey { func removeSSHKeys(sshKeys []*sshKey, removeSSHKeys []*sshKey) []*sshKey {
for i := range removeSSHKeys {
for j := range sshKeys { s := make([]*sshKey, 0)
if sshKeys[j].Compare(removeSSHKeys[i]) {
sshKeys = append(sshKeys[:j], sshKeys[j+1:]...) Loop:
for i := range sshKeys {
for j := range removeSSHKeys {
if sshKeys[i].Compare(removeSSHKeys[j]) {
continue Loop
} }
} }
s = append(s, sshKeys[i])
} }
return sshKeys
return s
} }
func rootCmd(cmd *cobra.Command, args []string) error { func rootCmd(cmd *cobra.Command, args []string) error {
@ -210,11 +216,7 @@ func writeSSHKeys(authorizedKeyFile string, sshKeys []*sshKey) error {
} }
for i := range sshKeys { for i := range sshKeys {
if len(sshKeys[i].alias) > 0 { fmt.Fprintln(f, sshKeys[i].String())
fmt.Fprintf(f, "%v %v %v\n", sshKeys[i].algorithm, sshKeys[i].pubKey, sshKeys[i].alias)
} else {
fmt.Fprintf(f, "%v %v\n", sshKeys[i].algorithm, sshKeys[i].pubKey)
}
} }
return nil return nil
@ -240,6 +242,14 @@ func (s *sshKey) SetAlias(alias string) {
} }
} }
func (s *sshKey) String() string {
l := fmt.Sprintf("%v %v", s.algorithm, s.pubKey)
if len(s.alias) > 0 {
l = fmt.Sprintf("%v %v", l, s.alias)
}
return l
}
func (s *sshKey) Validate() error { func (s *sshKey) Validate() error {
entries := map[string]string{ entries := map[string]string{
"algorithm": s.algorithm, "algorithm": s.algorithm,

58
main_test.go Normal file
View File

@ -0,0 +1,58 @@
package main
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestRemove(t *testing.T) {
require := require.New(t)
s := []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"},
{algorithm: "ssh-rsa", pubKey: "asdfasdadsad", alias: "world@hello"},
}
r := []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"},
}
result := removeSSHKeys(s, r)
require.Len(result, 1)
r = []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"},
{algorithm: "ssh-rsa", pubKey: "asdfasdadsad", alias: "world@hello"},
}
result = removeSSHKeys(s, r)
require.Len(result, 0)
r = []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "sdfsdf"},
}
result = removeSSHKeys(s, r)
require.Len(result, 1)
}
func TestSSHKeyString(t *testing.T) {
require := require.New(t)
s := []*sshKey{
{algorithm: "ssh-ed25519", pubKey: "sdfsdf", alias: "hello@world"},
{algorithm: "ssh-rsa", pubKey: "asdfasdadsad", alias: "world@hello"},
{algorithm: "ssh-rsa", pubKey: "asdfasdsdfsfadsad"},
}
b := []string{
"ssh-ed25519 sdfsdf hello@world",
"ssh-rsa asdfasdadsad world@hello",
"ssh-rsa asdfasdsdfsfadsad",
}
for i := range s {
require.Equal(b[i], s[i].String())
}
}