|
@ -19,7 +19,8 @@ import ( |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
type Authenticator struct { |
|
|
type Authenticator struct { |
|
|
PermitCommonNames map[string]bool |
|
|
AllowedWildcardDomain string |
|
|
|
|
|
AllowedCommonNames map[string]bool |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func LoadServerTLS(config *util.ViperProxy, component string) (grpc.ServerOption, grpc.ServerOption) { |
|
|
func LoadServerTLS(config *util.ViperProxy, component string) (grpc.ServerOption, grpc.ServerOption) { |
|
@ -49,14 +50,16 @@ func LoadServerTLS(config *util.ViperProxy, component string) (grpc.ServerOption |
|
|
ClientAuth: tls.RequireAndVerifyClientCert, |
|
|
ClientAuth: tls.RequireAndVerifyClientCert, |
|
|
}) |
|
|
}) |
|
|
|
|
|
|
|
|
permitCommonNames := strings.Split(config.GetString(component+".allowed_commonNames"), ",") |
|
|
allowedCommonNames := strings.Split(config.GetString(component+".allowed_commonNames"), ",") |
|
|
if len(permitCommonNames) > 0 { |
|
|
allowedWildcardDomain := config.GetString("grpc.allowed_wildcard_domain") |
|
|
permitCommonNamesMap := make(map[string]bool) |
|
|
if len(allowedCommonNames) > 0 || allowedWildcardDomain != "" { |
|
|
for _, s := range permitCommonNames { |
|
|
allowedCommonNamesMap := make(map[string]bool) |
|
|
permitCommonNamesMap[s] = true |
|
|
for _, s := range allowedCommonNames { |
|
|
|
|
|
allowedCommonNamesMap[s] = true |
|
|
} |
|
|
} |
|
|
auther := Authenticator{ |
|
|
auther := Authenticator{ |
|
|
PermitCommonNames: permitCommonNamesMap, |
|
|
AllowedCommonNames: allowedCommonNamesMap, |
|
|
|
|
|
AllowedWildcardDomain: allowedWildcardDomain, |
|
|
} |
|
|
} |
|
|
return grpc.Creds(ta), grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(auther.Authenticate)) |
|
|
return grpc.Creds(ta), grpc.UnaryInterceptor(grpc_auth.UnaryServerInterceptor(auther.Authenticate)) |
|
|
} |
|
|
} |
|
@ -109,9 +112,12 @@ func (a Authenticator) Authenticate(ctx context.Context) (newCtx context.Context |
|
|
if len(tlsAuth.State.VerifiedChains) == 0 || len(tlsAuth.State.VerifiedChains[0]) == 0 { |
|
|
if len(tlsAuth.State.VerifiedChains) == 0 || len(tlsAuth.State.VerifiedChains[0]) == 0 { |
|
|
return ctx, status.Error(codes.Unauthenticated, "could not verify peer certificate") |
|
|
return ctx, status.Error(codes.Unauthenticated, "could not verify peer certificate") |
|
|
} |
|
|
} |
|
|
|
|
|
commonName := tlsAuth.State.VerifiedChains[0][0].Subject.CommonName |
|
|
if _, ok := a.PermitCommonNames[tlsAuth.State.VerifiedChains[0][0].Subject.CommonName]; !ok { |
|
|
if a.AllowedWildcardDomain != "" && strings.HasSuffix(commonName, a.AllowedWildcardDomain) { |
|
|
return ctx, status.Error(codes.Unauthenticated, "invalid subject common name") |
|
|
return ctx, nil |
|
|
|
|
|
} |
|
|
|
|
|
if _, ok := a.AllowedCommonNames[commonName]; ok { |
|
|
|
|
|
return ctx, nil |
|
|
} |
|
|
} |
|
|
return ctx, nil |
|
|
return ctx, status.Error(codes.Unauthenticated, "invalid subject common name") |
|
|
} |
|
|
} |
xxxxxxxxxx