Skip to content

Commit

Permalink
Add function to delete list of credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Duchesne committed Jul 13, 2019
1 parent 11fd301 commit 6e69c09
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 57 deletions.
7 changes: 0 additions & 7 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/aws/aws-sdk-go v1.20.12 h1:xV7xfLSkiqd7JOnLlfER+Jz8kI98rAGJvtXssYkCRs4=
github.com/aws/aws-sdk-go v1.20.12/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/aws/aws-sdk-go v1.20.15 h1:y9ts8MJhB7ReUidS6Rq+0KxdFeL01J+pmOlGq6YqpiQ=
github.com/aws/aws-sdk-go v1.20.15/go.mod h1:KmX6BPdI08NWTb3/sm4ZGu5ShLoqVDhKgpiN924inxo=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
Expand Down Expand Up @@ -104,14 +102,11 @@ github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2 h1:VUFqw5KcqRf7i70GOzW7N+Q7+gxVBkSSqiXB12+JQ4M=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/spf13/viper v1.4.0 h1:yXHLWeravcrgGyFSyCgdYpXQ9dR9c/WED3pg1RhxqEU=
github.com/spf13/viper v1.4.0/go.mod h1:PTJ7Z/lr49W6bUbkmS1V3by4uWynFiR9p7+dSq/yZzE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down Expand Up @@ -144,7 +139,6 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -159,7 +153,6 @@ google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoA
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
google.golang.org/grpc v1.21.0/go.mod h1:oYelfM1adQP15Ek0mdvEgi9Df8B9CZIaU1084ijfRaM=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
32 changes: 14 additions & 18 deletions sync/config.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package sync

import (
"fmt"

"github.com/coveooss/credentials-sync/credentials"
"github.com/coveooss/credentials-sync/targets"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -38,7 +36,7 @@ func (config *Configuration) Sync() {
allTargets := config.Targets.AllTargets()
initChannel := make(chan targets.Target)
for _, target := range allTargets {
go initTarget(target, creds, initChannel, config.StopOnError)
go config.initTarget(target, creds, initChannel)
}
for i := 0; i < len(allTargets); i++ {
initTarget := <-initChannel
Expand All @@ -50,7 +48,7 @@ func (config *Configuration) Sync() {
syncChannel := make(chan bool, config.TargetParallelism)
for _, target := range validTargets {
syncChannel <- true
go syncCredentials(target, creds, syncChannel, config.StopOnError)
go config.syncCredentials(target, creds, syncChannel)
}

for i := 0; i < cap(syncChannel); i++ {
Expand All @@ -59,22 +57,25 @@ func (config *Configuration) Sync() {

}

func initTarget(target targets.Target, creds []credentials.Credentials, channel chan targets.Target, panicOnError bool) {
func (config *Configuration) logError(format string, args ...interface{}) {
if config.StopOnError {
log.Fatalf(format, args...)
}
log.Errorf(format, args...)
}

func (config *Configuration) initTarget(target targets.Target, creds []credentials.Credentials, channel chan targets.Target) {
err := target.Initialize(creds)
if err == nil {
log.Infof("Connected to %s", target.ToString())
channel <- target
} else {
message := fmt.Sprintf("Target `%s` has failed initialization: %v", target.GetName(), err)
if panicOnError {
log.Fatal(message)
}
log.Warning(message)
config.logError("Target `%s` has failed initialization: %v", target.GetName(), err)
channel <- nil
}
}

func syncCredentials(target targets.Target, credentialsList []credentials.Credentials, channel chan bool, panicOnError bool) {
func (config *Configuration) syncCredentials(target targets.Target, credentialsList []credentials.Credentials, channel chan bool) {
defer func() { <-channel }()

filteredCredentials := []credentials.Credentials{}
Expand All @@ -84,13 +85,8 @@ func syncCredentials(target targets.Target, credentialsList []credentials.Creden
}
}

if err := targets.UpdateListOfCredentials(target, filteredCredentials); err != nil {
message := fmt.Sprintf("Failed to send credentials to %s: %v", target.GetName(), err)
if panicOnError {
log.Fatal(message)
}
log.Error(message)
}
config.UpdateListOfCredentials(target, filteredCredentials)
config.DeleteListOfCredentials(target)

log.Infof("Finished sync to %s", target.GetName())
}
50 changes: 50 additions & 0 deletions sync/target.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package sync

import (
"github.com/coveooss/credentials-sync/credentials"
"github.com/coveooss/credentials-sync/targets"
log "github.com/sirupsen/logrus"
)

// DeleteListOfCredentials deletes the configured list of credentials from the given target
func (config *Configuration) DeleteListOfCredentials(target targets.Target) {
for _, id := range config.CredentialsToDelete {
if targets.HasCredential(target, id) {
log.Infof("[%s] Deleting %s", target.GetName(), id)
if err := target.DeleteCredentials(id); err != nil {
config.logError("Failed to delete credential %s from %s: %v", id, target.GetName(), err)
}
}
}
}

// UpdateListOfCredentials syncs the given list of credentials to the given target
func (config *Configuration) UpdateListOfCredentials(target targets.Target, listOfCredentials []credentials.Credentials) {
isSynced := func(id string) bool {
for _, credentials := range listOfCredentials {
if credentials.GetID() == id {
return true
}
}
return false
}

for _, credentials := range listOfCredentials {
log.Infof("[%s] Syncing %s", target.GetName(), credentials.GetID())
if err := target.UpdateCredentials(credentials); err != nil {
config.logError("Failed to send credentials with ID %s to %s: %v", credentials.GetID(), target.GetName(), err)
}
}

if target.ShouldDeleteUnsynced() {
log.Debugf("Deleting unsynced credentials from %v", target.GetName())
for _, existingID := range target.GetExistingCredentials() {
if !isSynced(existingID) {
log.Infof("[%s] Deleting %s", target.GetName(), existingID)
if err := target.DeleteCredentials(existingID); err != nil {
config.logError("Failed to delete credentials with ID %s to %s: %v", existingID, target.GetName(), err)
}
}
}
}
}
2 changes: 2 additions & 0 deletions targets/jenkins.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,12 @@ func (jenkins *JenkinsTarget) ToString() string {
return fmt.Sprintf("%s (Jenkins) - %s", jenkins.BaseToString(), jenkins.URL)
}

// GetExistingCredentials returns a list of all credential IDs on the target
func (jenkins *JenkinsTarget) GetExistingCredentials() []string {
return jenkins.existingCredentials
}

// DeleteCredentials deletes the credentials with the given ID on the target
func (jenkins *JenkinsTarget) DeleteCredentials(id string) error {
return jenkins.credentialsManager.Delete(credentialsDomain, id)
}
Expand Down
44 changes: 12 additions & 32 deletions targets/targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,6 @@ type Target interface {
ValidateConfiguration() bool
}

// UpdateListOfCredentials syncs the given list of credentials to the given target
func UpdateListOfCredentials(target Target, listOfCredentials []credentials.Credentials) error {
isSynced := func(id string) bool {
for _, credentials := range listOfCredentials {
if credentials.GetID() == id {
return true
}
}
return false
}

for _, credentials := range listOfCredentials {
log.Infof("[%s] Syncing %s", target.GetName(), credentials.GetID())
if err := target.UpdateCredentials(credentials); err != nil {
return err
}
}

if target.ShouldDeleteUnsynced() {
log.Debugf("Deleting unsynced credentials from %v", target.GetName())
for _, existingID := range target.GetExistingCredentials() {
if !isSynced(existingID) {
log.Infof("[%s] Deleting %s", target.GetName(), existingID)
if err := target.DeleteCredentials(existingID); err != nil {
return err
}
}
}
}
return nil
}

// Base contains attributes which are common to all targets
type Base struct {
DeleteUnsynced bool `mapstructure:"delete_unsynced"`
Expand Down Expand Up @@ -98,14 +66,26 @@ func (targetBase *Base) GetTags() map[string]string {
return targetBase.Tags
}

// ShouldDeleteUnsynced returns true if the unsynced credentials should be deleted from the target
func (targetBase *Base) ShouldDeleteUnsynced() bool {
return targetBase.DeleteUnsynced
}

// ShouldTagUnsynced returns true if the unsynced credentials should be tagged accordingly on the target
func (targetBase *Base) ShouldTagUnsynced() bool {
return targetBase.TagUnsynced
}

// HasCredential returns true if the given ID is found on the target
func HasCredential(target Target, id string) bool {
for _, existingId := range target.GetExistingCredentials() {
if existingId == id {
return true
}
}
return false
}

// Configuration contains all configured targets
type Configuration struct {
JenkinsTargets []*JenkinsTarget `mapstructure:"jenkins"`
Expand Down

0 comments on commit 6e69c09

Please sign in to comment.