Sergiusz Bazanski | b13b7ff | 2019-08-29 20:12:24 +0200 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 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 | |
| 18 | var ( |
| 19 | reUsername = regexp.MustCompile(`^[a-zA-Z0-9_\.]+$`) |
| 20 | ) |
| 21 | |
| 22 | func (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 Bazanski | 896926c | 2019-08-31 15:00:51 +0200 | [diff] [blame] | 41 | defer lconn.Close() |
Sergiusz Bazanski | b13b7ff | 2019-08-29 20:12:24 +0200 | [diff] [blame] | 42 | |
| 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 Bazanski | 0754ed8 | 2020-11-27 09:42:59 +0000 | [diff] [blame] | 66 | err = p.kubernetesSetupUser(ctx, username) |
Sergiusz Bazanski | b13b7ff | 2019-08-29 20:12:24 +0200 | [diff] [blame] | 67 | 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 Bazanski | f3312ef | 2020-08-01 17:15:52 +0200 | [diff] [blame] | 72 | kubernetesKeys, err := p.kubernetesCreds(username) |
Sergiusz Bazanski | b13b7ff | 2019-08-29 20:12:24 +0200 | [diff] [blame] | 73 | 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 Bazanski | f3312ef | 2020-08-01 17:15:52 +0200 | [diff] [blame] | 77 | |
Serge Bazanski | 0754ed8 | 2020-11-27 09:42:59 +0000 | [diff] [blame] | 78 | hspkiKeys, err := p.hspkiCreds(ctx, username) |
Serge Bazanski | f3312ef | 2020-08-01 17:15:52 +0200 | [diff] [blame] | 79 | 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 Bazanski | b13b7ff | 2019-08-29 20:12:24 +0200 | [diff] [blame] | 84 | return &pb.AuthenticateResponse{ |
| 85 | Result: pb.AuthenticateResponse_RESULT_AUTHENTICATED, |
Serge Bazanski | f3312ef | 2020-08-01 17:15:52 +0200 | [diff] [blame] | 86 | KubernetesKeys: kubernetesKeys, |
| 87 | HspkiKeys: hspkiKeys, |
Sergiusz Bazanski | b13b7ff | 2019-08-29 20:12:24 +0200 | [diff] [blame] | 88 | }, nil |
| 89 | } |
| 90 | |
| 91 | func (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 | } |