Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user Regex and Allow Group prefixes #29

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func initConfig() {
viper.SetEnvPrefix("ssosync")
viper.AutomaticEnv()

for _, e := range []string{"google_admin", "google_credentials", "scim_access_token", "scim_endpoint", "log_level", "log_format", "ignore_users", "ignore_groups"} {
for _, e := range []string{"google_admin", "google_credentials", "scim_access_token", "scim_endpoint", "log_level", "log_format", "ignore_users", "ignore_groups", "allow_groups", "allow_pattern"} {
if err := viper.BindEnv(e); err != nil {
log.Fatalf(errors.Wrap(err, "cannot bind environment variable").Error())
}
Expand Down Expand Up @@ -154,6 +154,8 @@ func addFlags(cmd *cobra.Command, cfg *config.Config) {
rootCmd.Flags().StringVarP(&cfg.GoogleAdmin, "google-admin", "u", "", "Google Admin Email")
rootCmd.Flags().StringSliceVar(&cfg.IgnoreUsers, "ignore-users", []string{}, "ignores these users")
rootCmd.Flags().StringSliceVar(&cfg.IgnoreGroups, "ignore-groups", []string{}, "ignores these groups")
rootCmd.Flags().StringSliceVar(&cfg.AllowGroups, "allow-groups", []string{}, "allows only these groups (prefixed with this)")
rootCmd.Flags().StringSliceVar(&cfg.AllowPattern, "allow-pattern", []string{}, "pattern necessary for a user email to be allowed")
}

func logConfig(cfg *config.Config) {
Expand Down
4 changes: 4 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type Config struct {
IgnoreUsers []string `mapstructure:"ignore_users"`
// Ignore groups ...
IgnoreGroups []string `mapstructure:"ignore_groups"`
// Allow Groups
AllowGroups []string `mapstructure:"allow_groups"`
// Allow Pattern
AllowPattern []string `mapstructure:"allow_pattern"`
}

const (
Expand Down
20 changes: 15 additions & 5 deletions internal/config/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package config

import (
"encoding/base64"

"os"
log "github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/secretsmanager"
)
Expand All @@ -19,24 +20,33 @@ func NewSecrets(svc *secretsmanager.SecretsManager) *Secrets {
}
}

func (s *Secrets) getSecretByNameOrEnv(name string, envName string) (string, error) {
secretName := name
if len(os.Getenv(envName)) > 0 {
secretName = os.Getenv(envName)
}
log.Debug("Getting Secret Name: ", secretName)
return s.getSecret(secretName)
}

// GoogleAdminEmail ...
func (s *Secrets) GoogleAdminEmail() (string, error) {
return s.getSecret("SSOSyncGoogleAdminEmail")
return s.getSecretByNameOrEnv("SSOSyncGoogleAdminEmail", "SSOSYNC_SECRETS_GOOGLE_EMAIL")
}

// SCIMAccessToken ...
func (s *Secrets) SCIMAccessToken() (string, error) {
return s.getSecret("SSOSyncSCIMAccessToken")
return s.getSecretByNameOrEnv("SSOSyncSCIMAccessToken", "SSOSYNC_SECRETS_SCIM_TOKEN")
}

// SCIMEndpointUrl ...
func (s *Secrets) SCIMEndpointUrl() (string, error) {
return s.getSecret("SSOSyncSCIMEndpointUrl")
return s.getSecretByNameOrEnv("SSOSyncSCIMEndpointUrl", "SSOSYNC_SECRETS_SCIM_URL")
}

// GoogleCredentials ...
func (s *Secrets) GoogleCredentials() (string, error) {
return s.getSecret("SSOSyncGoogleCredentials")
return s.getSecretByNameOrEnv("SSOSyncGoogleCredentials", "SSOSYNC_SECRETS_GOOGLE_CREDENTIALS")
}

func (s *Secrets) getSecret(secretKey string) (string, error) {
Expand Down
42 changes: 42 additions & 0 deletions internal/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ package internal

import (
"context"
"strings"
"io/ioutil"
"regexp"

"github.com/awslabs/ssosync/internal/aws"
"github.com/awslabs/ssosync/internal/config"
Expand Down Expand Up @@ -100,6 +102,11 @@ func (s *syncGSuite) SyncUsers() error {
continue
}

if !s.allowPattern(u.PrimaryEmail) {
log.Debug("Filtered out a user")
continue
}

ll := log.WithFields(log.Fields{
"email": u.PrimaryEmail,
})
Expand Down Expand Up @@ -156,6 +163,10 @@ func (s *syncGSuite) SyncGroups() error {
continue
}

if ! s.allowGroup(g.Email) {
Copy link

@IDisposable IDisposable Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit This one place you've got a blank after the bang !... maybe delete that?

continue
}

log := log.WithFields(log.Fields{
"group": g.Email,
})
Expand Down Expand Up @@ -296,3 +307,34 @@ func (s *syncGSuite) ignoreGroup(name string) bool {

return false
}

func (s *syncGSuite) allowGroup(name string) bool {
if len(s.cfg.AllowGroups) == 0 {
return true
}

for _, g := range s.cfg.AllowGroups {
if strings.HasPrefix(name, g) {
return true
}
}

return false
}

func (s *syncGSuite) allowPattern(name string) bool {
if len(s.cfg.AllowGroups) == 0 {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be testing the length of s.cfg.AllowPatterns instead?

return true
}
for _, p := range s.cfg.AllowPattern {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit Everywhere else you've inserted a blank line before the for, maybe insert one here too?

if p == "" {
Copy link

@IDisposable IDisposable Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would accepting a pattern of "*" be less accident prone than simply a blank pattern? Seems too easy for an errant comma causing everyone to match.

return true
}

re := regexp.MustCompile(p)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this going to be a performance problem with lots of patterns compiled and applied at each user? Should we really be preprocessing the patterns once to compile them (newish to go)

if re.FindStringIndex(name) != nil {
return true
}
}
return false
}