-
Notifications
You must be signed in to change notification settings - Fork 613
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add checks to backend for terminal proxy
Access to commandline terminals (cloud shells) has to be restricted to avoid privilege escalation issues. This commit adds the following checks: - Ensure webhooks expected by che-workspace-operator are installed - Ensures the user requesting a terminal is not a cluster admin (done by checking if they can read pods in a privileged namespace (openshift-operators). Adds an endpoint: /api/terminal/available/ that can be used to check if basic requirements for the proxy are met (workspace operator is running) Signed-off-by: Angel Misevski <amisevsk@redhat.com>
- Loading branch information
Showing
6 changed files
with
180 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package terminal | ||
|
||
import ( | ||
authv1 "k8s.io/api/authorization/v1" | ||
) | ||
|
||
func (p *Proxy) checkUserPermissions(token string) (bool, error) { | ||
client, err := p.createTypedClient(token) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
sar := &authv1.SelfSubjectAccessReview{ | ||
Spec: authv1.SelfSubjectAccessReviewSpec{ | ||
ResourceAttributes: &authv1.ResourceAttributes{ | ||
Namespace: "openshift-operators", | ||
Verb: "get", | ||
Resource: "pods", | ||
}, | ||
}, | ||
} | ||
res, err := client.AuthorizationV1().SelfSubjectAccessReviews().Create(sar) | ||
if err != nil || res == nil { | ||
return false, err | ||
} | ||
return !res.Status.Allowed, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package terminal | ||
|
||
import ( | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
// createDynamicClient create dynamic client with the configured token to be used | ||
func (p *Proxy) createDynamicClient(token string) (dynamic.Interface, error) { | ||
var tlsClientConfig rest.TLSClientConfig | ||
if p.TLSClientConfig.InsecureSkipVerify { | ||
// off-cluster mode | ||
tlsClientConfig.Insecure = true | ||
} else { | ||
inCluster, err := rest.InClusterConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
tlsClientConfig = inCluster.TLSClientConfig | ||
} | ||
|
||
config := &rest.Config{ | ||
Host: p.ClusterEndpoint.Host, | ||
TLSClientConfig: tlsClientConfig, | ||
BearerToken: token, | ||
} | ||
|
||
client, err := dynamic.NewForConfig(dynamic.ConfigFor(config)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return client, nil | ||
} | ||
|
||
func (p *Proxy) createTypedClient(token string) (*kubernetes.Clientset, error) { | ||
var tlsClientConfig rest.TLSClientConfig | ||
if p.TLSClientConfig.InsecureSkipVerify { | ||
// off-cluster mode | ||
tlsClientConfig.Insecure = true | ||
} else { | ||
inCluster, err := rest.InClusterConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
tlsClientConfig = inCluster.TLSClientConfig | ||
} | ||
|
||
config := &rest.Config{ | ||
Host: p.ClusterEndpoint.Host, | ||
TLSClientConfig: tlsClientConfig, | ||
BearerToken: token, | ||
} | ||
|
||
return kubernetes.NewForConfig(config) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package terminal | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/api/errors" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/rest" | ||
) | ||
|
||
const ( | ||
webhookName = "workspace.che.eclipse.org" | ||
) | ||
|
||
func workspaceOperatorIsRunning() (bool, error) { | ||
config, err := rest.InClusterConfig() | ||
if err != nil { | ||
return false, err | ||
} | ||
client, err := kubernetes.NewForConfig(config) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
_, err = client.AdmissionregistrationV1().MutatingWebhookConfigurations().Get(webhookName, metav1.GetOptions{}) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
return false, nil | ||
} | ||
return false, err | ||
} | ||
|
||
_, err = client.AdmissionregistrationV1().ValidatingWebhookConfigurations().Get(webhookName, metav1.GetOptions{}) | ||
if err != nil { | ||
if errors.IsNotFound(err) { | ||
return false, nil | ||
} | ||
return false, err | ||
} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters