-
Notifications
You must be signed in to change notification settings - Fork 4
[WIP] authenticated microservices #1238
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 9 commits
8009db2
d75fe3c
8796ed7
3c4d60a
90c27ca
b501eda
12f01d9
bb7f1de
8e27f7d
6121181
2b0900d
415c4f6
1f0e8f8
09df217
c235283
5c12d11
49a25a3
1de5b2f
86dd0a9
e390516
a612f55
148e053
f9c0984
6f33194
23bead9
bc037cc
6b048b6
bdf937a
698e2a5
2579328
20c7171
fdac9db
b69b3e4
39849e7
c288726
e53392d
2a6fd31
7a04db0
15ae7e3
92c4e01
cd60fe4
b20dda2
b9959bc
eca3464
45a0c3b
4050086
3c1ff88
ec79fd3
fd0d57a
7a25619
fee4a56
0e2ee9b
c1a97c0
a2da43b
d7c18a8
105231c
7a390ea
d5581c8
454d67b
878822d
4844e9b
ff458a2
b42254b
b5e581c
9566b3a
6a3584c
e3b9d9c
2afa564
aa06a2c
4ae4edd
ac57556
9c3d918
804dffa
dce76ae
467483c
f1265ff
64bd7df
bbd1b34
64ac8f2
2e24b08
bc4fb35
5813501
df910bb
11a89f5
d9f1f31
ee6e5a1
5c3c681
925d062
fb80354
76ce335
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| package com.saveourtool.save.backend.utils | ||
|
|
||
| import com.saveourtool.save.utils.debug | ||
| import com.saveourtool.save.utils.getLogger | ||
| import io.fabric8.kubernetes.api.model.authentication.TokenReview | ||
| import io.fabric8.kubernetes.client.KubernetesClient | ||
| import io.fabric8.kubernetes.client.utils.Serialization | ||
| import org.intellij.lang.annotations.Language | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnCloudPlatform | ||
| import org.springframework.boot.cloud.CloudPlatform | ||
| import org.springframework.security.authentication.ReactiveAuthenticationManager | ||
| import org.springframework.security.core.Authentication | ||
| import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken | ||
| import org.springframework.security.web.server.authentication.ServerAuthenticationConverter | ||
| import org.springframework.stereotype.Component | ||
| import org.springframework.web.server.ServerWebExchange | ||
| import reactor.core.publisher.Mono | ||
|
|
||
| @Component | ||
| @ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) | ||
| class ServiceAccountTokenExtractorConverter : ServerAuthenticationConverter { | ||
| override fun convert(exchange: ServerWebExchange): Mono<Authentication> { | ||
| return Mono.justOrEmpty( | ||
| exchange.request.headers["X-Service-Account-Token"]?.firstOrNull() | ||
| ).map { token -> | ||
| PreAuthenticatedAuthenticationToken("TokenSupplier", token) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| @Component | ||
| @ConditionalOnCloudPlatform(CloudPlatform.KUBERNETES) | ||
| class ServiceAccountAuthenticatingManager( | ||
| // val kubernetesClient: ApiClient, | ||
| val kubernetesClient: KubernetesClient, | ||
| ) : ReactiveAuthenticationManager { | ||
| override fun authenticate(authentication: Authentication): Mono<Authentication> { | ||
| val token = (authentication as PreAuthenticatedAuthenticationToken).credentials | ||
| @Language("yaml") | ||
| val tokenReview = """ | ||
| |apiVersion: authentication.k8s.io/v1 | ||
| |kind: TokenReview | ||
| |metadata: | ||
| | name: service-account-validity-check | ||
| | namespace: ${kubernetesClient.namespace} | ||
| |spec: | ||
| | token: $token | ||
| """.trimMargin() | ||
| /* val tokenReview = V1TokenReview().apply { | ||
| spec = V1TokenReviewSpec().apply { | ||
| setToken(token) | ||
| } | ||
| } | ||
| AuthenticationV1Api(kubernetesClient).createTokenReview(tokenReview)*/ | ||
| logger.debug { | ||
| "Will create k8s resource from the following YAML:\n${tokenReview.prependIndent(" ")}" | ||
| } | ||
| val response = kubernetesClient.resource(tokenReview).createOrReplace() as TokenReview | ||
| logger.debug { | ||
| "Got the following response from the API server:\n${Serialization.yamlMapper().writeValueAsString(response).prependIndent(" ")}" | ||
| } | ||
| authentication.isAuthenticated = response.status.error == null && response.status.authenticated | ||
| return Mono.just(authentication) | ||
| } | ||
|
|
||
| private val logger = getLogger<ServiceAccountAuthenticatingManager>() | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,7 @@ spec: | |
| annotations: | ||
| {{- include "pod.common.annotations" (dict "service" .Values.backend ) | nindent 8 }} | ||
| spec: | ||
| serviceAccountName: microservice-sa | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can see, all the microservices that are able to send requests should have Moreover, it seems that orchestrator and demo role bindings should also reference
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea was, that communication between some microservices can still be secure without security, because we were planning to account only for not trusted executed tools and their network activity could be restricted with a
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As far as I can see, many things have been done:
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose it was already more or less working. This PR affects only services, not agents, and I'm not sure if we have ktor on server-side. But yes, the part that validates token against k8s api server requires |
||
| restartPolicy: Always | ||
| {{- include "cnb.securityContext" . | nindent 6 }} | ||
| containers: | ||
|
|
@@ -35,6 +36,7 @@ spec: | |
| mountPath: /home/cnb/files | ||
| - name: database-secret | ||
| mountPath: {{ .Values.mysql.dbPasswordFile }} | ||
| - {{ include "spring-boot.sa-token-mount" . | indent 14 | trim }} | ||
| {{- include "spring-boot.management" .Values.backend | nindent 10 }} | ||
| resources: | ||
| limits: | ||
|
|
@@ -109,3 +111,4 @@ spec: | |
| secretName: db-secrets | ||
| - name: migrations-data | ||
| emptyDir: {} | ||
| - {{ include "spring-boot.sa-token-volume" (dict "service" .Values.backend) | indent 10 | trim }} | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| apiVersion: v1 | ||
| kind: ServiceAccount | ||
| metadata: | ||
| name: microservice-sa | ||
|
|
||
| --- | ||
|
|
||
| # https://docs.spring.io/spring-cloud-kubernetes/docs/current/reference/html/#service-account | ||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: Role | ||
| metadata: | ||
| name: microservice | ||
| rules: | ||
| - apiGroups: [""] # "" indicates the core API group | ||
| resources: [configmaps, secrets] | ||
| verbs: [list, get, watch] | ||
|
|
||
| --- | ||
|
|
||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: RoleBinding | ||
| metadata: | ||
| name: microservice-role-binding | ||
| subjects: | ||
| - kind: ServiceAccount | ||
| name: microservice-sa | ||
| roleRef: | ||
| apiGroup: rbac.authorization.k8s.io | ||
| kind: Role | ||
| name: microservice | ||
|
|
||
| --- | ||
|
|
||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: ClusterRole | ||
| metadata: | ||
| name: microservice | ||
| rules: | ||
| - apiGroups: ["authentication.k8s.io"] | ||
| resources: [tokenreviews] | ||
| verbs: ["create"] | ||
|
|
||
| --- | ||
|
|
||
| apiVersion: rbac.authorization.k8s.io/v1 | ||
| kind: ClusterRoleBinding | ||
| metadata: | ||
| name: microservice-role-binding | ||
| subjects: | ||
| - kind: ServiceAccount | ||
| name: microservice-sa | ||
| namespace: {{ .Release.Namespace }} | ||
| roleRef: | ||
| apiGroup: rbac.authorization.k8s.io | ||
| kind: ClusterRole | ||
| name: microservice |
Uh oh!
There was an error while loading. Please reload this page.