浏览代码

Make SanitizeSSHPrincipal a public function.

Mariano Cano 7 年之前
父节点
当前提交
48c98dea2a
共有 3 个文件被更改,包括 30 次插入 和 21 次删除
  1. 1 19
      authority/provisioner/oidc.go
  2. 26 2
      authority/provisioner/provisioner.go
  3. 3 0
      authority/provisioner/sign_ssh_options.go

+ 1 - 19
authority/provisioner/oidc.go

@@ -309,7 +309,7 @@ func (o *OIDC) authorizeSSHSign(claims *openIDPayload) ([]SignOption, error) {
 	if o.IsAdmin(claims.Email) {
 		signOptions = append(signOptions, &sshCertificateOptionsValidator{})
 	} else {
-		name := principalFromEmail(claims.Email)
+		name := SanitizeSSHPrincipal(claims.Email)
 		if !sshUserRegex.MatchString(name) {
 			return nil, errors.Errorf("invalid principal '%s' from email address '%s'", name, claims.Email)
 		}
@@ -338,21 +338,3 @@ func getAndDecode(uri string, v interface{}) error {
 	}
 	return nil
 }
-
-func principalFromEmail(email string) string {
-	if i := strings.LastIndex(email, "@"); i >= 0 {
-		email = email[:i]
-	}
-	return strings.Map(func(r rune) rune {
-		switch {
-		case r >= 'a' && r <= 'z':
-			return r
-		case r >= '0' && r <= '9':
-			return r
-		case r == '.': // drop dots
-			return -1
-		default:
-			return '_'
-		}
-	}, strings.ToLower(email))
-}

+ 26 - 2
authority/provisioner/provisioner.go

@@ -11,8 +11,6 @@ import (
 	"github.com/pkg/errors"
 )
 
-var sshUserRegex = regexp.MustCompile("^[a-z][-a-z0-9_]*$")
-
 // Interface is the interface that all provisioner types must implement.
 type Interface interface {
 	GetID() string
@@ -164,3 +162,29 @@ func (l *List) UnmarshalJSON(data []byte) error {
 
 	return nil
 }
+
+var sshUserRegex = regexp.MustCompile("^[a-z][-a-z0-9_]*$")
+
+// SanitizeSSHPrincipal grabs an email or a string with the format local@domain
+// and returns a sanitized version of the local, valid to be used as a user
+// name. If the email starts with a letter between a and z, the resulting string
+// will match the regular expression `^[a-z][-a-z0-9_]*$`.
+func SanitizeSSHPrincipal(email string) string {
+	if i := strings.LastIndex(email, "@"); i >= 0 {
+		email = email[:i]
+	}
+	return strings.Map(func(r rune) rune {
+		switch {
+		case r >= 'a' && r <= 'z':
+			return r
+		case r >= '0' && r <= '9':
+			return r
+		case r == '-':
+			return '-'
+		case r == '.': // drop dots
+			return -1
+		default:
+			return '_'
+		}
+	}, strings.ToLower(email))
+}

+ 3 - 0
authority/provisioner/sign_ssh_options.go

@@ -211,6 +211,9 @@ type sshCertificateOptionsValidator struct {
 // Valid implements SSHCertificateOptionsValidator and returns nil if both
 // SSHOptions match.
 func (v *sshCertificateOptionsValidator) Valid(got SSHOptions) error {
+	if v.Want == nil {
+		return nil
+	}
 	return v.Want.match(got)
 }