|
| 1 | +package openshift |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "net/http" |
| 8 | + "strings" |
| 9 | + |
| 10 | + kapi "github.com/GoogleCloudPlatform/kubernetes/pkg/api" |
| 11 | + log "github.com/Sirupsen/logrus" |
| 12 | + ctxu "github.com/docker/distribution/context" |
| 13 | + repoauth "github.com/docker/distribution/registry/auth" |
| 14 | + authorizationapi "github.com/openshift/origin/pkg/authorization/api" |
| 15 | + "github.com/openshift/origin/pkg/dockerregistry" |
| 16 | + "golang.org/x/net/context" |
| 17 | +) |
| 18 | + |
| 19 | +func init() { |
| 20 | + repoauth.Register("openshift", repoauth.InitFunc(newAccessController)) |
| 21 | +} |
| 22 | + |
| 23 | +type AccessController struct { |
| 24 | + UserRegistryConfig *dockerregistry.UserRegistryConfig |
| 25 | +} |
| 26 | + |
| 27 | +type authChallenge struct { |
| 28 | + err error |
| 29 | +} |
| 30 | + |
| 31 | +type OpenShiftAccess struct { |
| 32 | + Namespace string |
| 33 | + ImageRepo string |
| 34 | + Verb string |
| 35 | + BearerToken string |
| 36 | +} |
| 37 | + |
| 38 | +var _ repoauth.AccessController = &AccessController{} |
| 39 | +var _ repoauth.Challenge = &authChallenge{} |
| 40 | + |
| 41 | +// Errors used and exported by this package. |
| 42 | +var ( |
| 43 | + ErrTokenRequired = errors.New("authorization header with basic token required") |
| 44 | + ErrTokenInvalid = errors.New("failed to decode basic token") |
| 45 | + ErrOpenShiftTokenRequired = errors.New("expected openshift bearer token as password for basic token to registry") |
| 46 | + ErrNamespaceRequired = errors.New("repository namespace required") |
| 47 | + ErrOpenShiftAccessDenied = errors.New("openshift access denied") |
| 48 | +) |
| 49 | + |
| 50 | +func newAccessController(options map[string]interface{}) (repoauth.AccessController, error) { |
| 51 | + fmt.Println("Using OpenShift Auth handler") |
| 52 | + |
| 53 | + var rc dockerregistry.UserRegistryConfig |
| 54 | + err := rc.SetRegistryConfig() |
| 55 | + if err != nil { |
| 56 | + return nil, err |
| 57 | + } |
| 58 | + return &AccessController{ |
| 59 | + UserRegistryConfig: &rc, |
| 60 | + }, nil |
| 61 | +} |
| 62 | + |
| 63 | +// Error returns the internal error string for this authChallenge. |
| 64 | +func (ac *authChallenge) Error() string { |
| 65 | + return ac.err.Error() |
| 66 | +} |
| 67 | + |
| 68 | +// challengeParams constructs the value to be used in |
| 69 | +// the WWW-Authenticate response challenge header. |
| 70 | +// See https://tools.ietf.org/html/rfc6750#section-3 |
| 71 | +func (ac *authChallenge) challengeParams() string { |
| 72 | + return fmt.Sprintf("Basic realm=openshift error=%s", ac.Error()) |
| 73 | +} |
| 74 | + |
| 75 | +// ServeHttp handles writing the challenge response |
| 76 | +// by setting the challenge header and status code. |
| 77 | +func (ac *authChallenge) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 78 | + w.Header().Add("WWW-Authenticate", ac.challengeParams()) |
| 79 | + w.WriteHeader(http.StatusUnauthorized) |
| 80 | +} |
| 81 | + |
| 82 | +// Authorized handles checking whether the given request is authorized |
| 83 | +// for actions on resources allowed by openshift. |
| 84 | +func (ac *AccessController) Authorized(ctx context.Context, accessRecords ...repoauth.Access) (context.Context, error) { |
| 85 | + req, err := ctxu.GetRequest(ctx) |
| 86 | + if err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + challenge := &authChallenge{} |
| 90 | + |
| 91 | + authParts := strings.SplitN(req.Header.Get("Authorization"), " ", 2) |
| 92 | + if len(authParts) != 2 || strings.ToLower(authParts[0]) != "basic" { |
| 93 | + challenge.err = ErrTokenRequired |
| 94 | + return nil, challenge |
| 95 | + } |
| 96 | + basicToken := authParts[1] |
| 97 | + |
| 98 | + bearerToken := "" |
| 99 | + for _, access := range accessRecords { |
| 100 | + log.Debugf("%s:%s:%s", access.Resource.Type, access.Resource.Name, access.Action) |
| 101 | + |
| 102 | + if access.Resource.Type != "repository" { |
| 103 | + continue |
| 104 | + } |
| 105 | + |
| 106 | + if len(bearerToken) == 0 { |
| 107 | + payload, err := base64.StdEncoding.DecodeString(basicToken) |
| 108 | + if err != nil { |
| 109 | + log.Errorf("Basic token decode failed: %s", err) |
| 110 | + challenge.err = ErrTokenInvalid |
| 111 | + return nil, challenge |
| 112 | + } |
| 113 | + authParts = strings.SplitN(string(payload), ":", 2) |
| 114 | + if len(authParts) != 2 { |
| 115 | + challenge.err = ErrOpenShiftTokenRequired |
| 116 | + return nil, challenge |
| 117 | + } |
| 118 | + bearerToken = authParts[1] |
| 119 | + } |
| 120 | + |
| 121 | + repoParts := strings.SplitN(access.Resource.Name, "/", 2) |
| 122 | + if len(repoParts) != 2 { |
| 123 | + challenge.err = ErrNamespaceRequired |
| 124 | + return nil, challenge |
| 125 | + } |
| 126 | + osAccess := &OpenShiftAccess{ |
| 127 | + Namespace: repoParts[0], |
| 128 | + ImageRepo: repoParts[1], |
| 129 | + BearerToken: bearerToken, |
| 130 | + } |
| 131 | + |
| 132 | + switch access.Action { |
| 133 | + case "push": |
| 134 | + osAccess.Verb = "create" |
| 135 | + case "pull": |
| 136 | + osAccess.Verb = "get" |
| 137 | + default: |
| 138 | + challenge.err = fmt.Errorf("Unkown action: %s", access.Action) |
| 139 | + return nil, challenge |
| 140 | + } |
| 141 | + |
| 142 | + err = VerifyOpenShiftAccess(osAccess, ac) |
| 143 | + if err != nil { |
| 144 | + challenge.err = err |
| 145 | + return nil, challenge |
| 146 | + } |
| 147 | + } |
| 148 | + return context.WithValue(ctx, "BearerToken", bearerToken), nil |
| 149 | +} |
| 150 | + |
| 151 | +func VerifyOpenShiftAccess(osAccess *OpenShiftAccess, ac *AccessController) error { |
| 152 | + client, err := ac.UserRegistryConfig.GetRegistryClient(osAccess.BearerToken) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + sar := authorizationapi.SubjectAccessReview{ |
| 157 | + TypeMeta: kapi.TypeMeta{ |
| 158 | + APIVersion: "v1beta1", |
| 159 | + Kind: "SubjectAccessReview", |
| 160 | + }, |
| 161 | + Verb: osAccess.Verb, |
| 162 | + Resource: "imageRepositories", |
| 163 | + ResourceName: osAccess.ImageRepo, |
| 164 | + } |
| 165 | + response, err := client.SubjectAccessReviews(osAccess.Namespace).Create(&sar) |
| 166 | + if err != nil { |
| 167 | + log.Errorf("OpenShift client error: %s", err) |
| 168 | + return ErrOpenShiftAccessDenied |
| 169 | + } |
| 170 | + if !response.Allowed { |
| 171 | + log.Errorf("OpenShift access denied: %s", response.Reason) |
| 172 | + return ErrOpenShiftAccessDenied |
| 173 | + } |
| 174 | + return nil |
| 175 | +} |
0 commit comments