-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5332 from sleshchenko/apiTerminal
Bug 1839621: Add an ability to proxy requests to Che Workspace
- Loading branch information
Showing
25 changed files
with
411 additions
and
5,106 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
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,30 @@ | ||
package terminal | ||
|
||
import ( | ||
authv1 "k8s.io/api/authorization/v1" | ||
) | ||
|
||
// checkUserPermissions checks if the terminal proxy is supported for a given user. | ||
// Returns true if we're willing to proxy the user's token, false otherwise. We don't | ||
// want to proxy highly privileged tokens to avoid privilege escalation issues. | ||
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: "create", | ||
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,50 @@ | ||
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) { | ||
config, err := p.getConfig(token) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
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) { | ||
config, err := p.getConfig(token) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return kubernetes.NewForConfig(config) | ||
} | ||
|
||
func (p *Proxy) getConfig(token string) (*rest.Config, 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 | ||
} | ||
|
||
return &rest.Config{ | ||
Host: p.ClusterEndpoint.Host, | ||
TLSClientConfig: tlsClientConfig, | ||
BearerToken: token, | ||
}, 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,42 @@ | ||
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" | ||
) | ||
|
||
// workspaceOperatorIsRunning checks if the workspace operator is running and webhooks are enabled, | ||
// which is a prerequisite for sending a user's token to a workspace. | ||
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 | ||
} |
Oops, something went wrong.