blob: 160f260852d071d5e76a652b38f74a8f48a047de [file] [log] [blame]
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +02001package main
2
3import (
4 "context"
5 "crypto/tls"
6 "fmt"
7 "regexp"
8 "strings"
9
10 "github.com/golang/glog"
11 "google.golang.org/grpc/codes"
12 "google.golang.org/grpc/status"
13 ldap "gopkg.in/ldap.v3"
14
15 pb "code.hackerspace.pl/hscloud/cluster/prodvider/proto"
16)
17
18var (
19 reUsername = regexp.MustCompile(`^[a-zA-Z0-9_\.]+$`)
20)
21
22func (p *prodvider) Authenticate(ctx context.Context, req *pb.AuthenticateRequest) (*pb.AuthenticateResponse, error) {
23 username := strings.TrimSpace(req.Username)
24 if username == "" || !reUsername.MatchString(username) {
25 return nil, status.Error(codes.InvalidArgument, "invalid username")
26 }
27
28 password := req.Password
29 if password == "" {
30 return &pb.AuthenticateResponse{
31 Result: pb.AuthenticateResponse_RESULT_INVALID_CREDENTIALS,
32 }, nil
33 }
34
35 tlsConfig := &tls.Config{}
36 lconn, err := ldap.DialTLS("tcp", flagLDAPServer, tlsConfig)
37 if err != nil {
38 glog.Errorf("ldap.DialTLS: %v", err)
39 return nil, status.Error(codes.Unavailable, "could not context LDAP")
40 }
Sergiusz Bazanski896926c2019-08-31 15:00:51 +020041 defer lconn.Close()
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020042
43 dn := fmt.Sprintf(flagLDAPBindDN, username)
44 err = lconn.Bind(dn, password)
45
46 if err != nil {
47 if ldap.IsErrorWithCode(err, ldap.LDAPResultInvalidCredentials) {
48 return &pb.AuthenticateResponse{
49 Result: pb.AuthenticateResponse_RESULT_INVALID_CREDENTIALS,
50 }, nil
51 }
52
53 glog.Errorf("ldap.Bind: %v", err)
54 return nil, status.Error(codes.Unavailable, "could not query LDAP")
55 }
56
57 groups, err := p.groupMemberships(lconn, username)
58 if err != nil {
59 return nil, err
60 }
61
62 if !groups["kubernetes-users"] && !groups["staff"] {
63 return nil, status.Error(codes.PermissionDenied, "not part of staff or kubernetes-users")
64 }
65
Serge Bazanski0754ed82020-11-27 09:42:59 +000066 err = p.kubernetesSetupUser(ctx, username)
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020067 if err != nil {
68 glog.Errorf("kubernetesSetupUser(%v): %v", username, err)
69 return nil, status.Error(codes.Unavailable, "could not set up objects in Kubernetes")
70 }
71
Serge Bazanskif3312ef2020-08-01 17:15:52 +020072 kubernetesKeys, err := p.kubernetesCreds(username)
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020073 if err != nil {
74 glog.Errorf("kubernetesCreds(%q): %v", username, err)
75 return nil, status.Error(codes.Unavailable, "could not generate k8s keys")
76 }
Serge Bazanskif3312ef2020-08-01 17:15:52 +020077
Serge Bazanski0754ed82020-11-27 09:42:59 +000078 hspkiKeys, err := p.hspkiCreds(ctx, username)
Serge Bazanskif3312ef2020-08-01 17:15:52 +020079 if err != nil {
80 glog.Errorf("hspkiCreds(%q): %v", username, err)
81 return nil, status.Error(codes.Unavailable, "could not generate hspki keys")
82 }
83
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020084 return &pb.AuthenticateResponse{
85 Result: pb.AuthenticateResponse_RESULT_AUTHENTICATED,
Serge Bazanskif3312ef2020-08-01 17:15:52 +020086 KubernetesKeys: kubernetesKeys,
87 HspkiKeys: hspkiKeys,
Sergiusz Bazanskib13b7ff2019-08-29 20:12:24 +020088 }, nil
89}
90
91func (p *prodvider) groupMemberships(lconn *ldap.Conn, username string) (map[string]bool, error) {
92 searchRequest := ldap.NewSearchRequest(
93 flagLDAPGroupSearchBase,
94 ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
95 fmt.Sprintf("(uniqueMember=%s)", fmt.Sprintf(flagLDAPBindDN, username)),
96 []string{"dn", "cn"},
97 nil,
98 )
99
100 sr, err := lconn.Search(searchRequest)
101 if err != nil {
102 glog.Errorf("ldap.Search: %v", err)
103 return nil, status.Error(codes.Unavailable, "could not query LDAP for group")
104 }
105
106 res := make(map[string]bool)
107 for _, entry := range sr.Entries {
108 cn := entry.GetAttributeValue("cn")
109 res[cn] = true
110 }
111
112 return res, nil
113}