|
24 | 24 | import com.marcnuri.yakc.model.io.k8s.api.core.v1.Secret;
|
25 | 25 | import com.marcnuri.yakc.model.io.k8s.api.core.v1.ServiceAccount;
|
26 | 26 | import org.junit.jupiter.api.BeforeAll;
|
27 |
| -import org.junit.jupiter.api.MethodOrderer.OrderAnnotation; |
28 |
| -import org.junit.jupiter.api.Order; |
29 | 27 | import org.junit.jupiter.api.Test;
|
30 |
| -import org.junit.jupiter.api.TestMethodOrder; |
31 | 28 | import org.junit.jupiter.api.extension.ExtendWith;
|
32 | 29 |
|
33 | 30 | import java.io.IOException;
|
|
38 | 35 | /**
|
39 | 36 | * Created by Marc Nuri on 2020-05-02.
|
40 | 37 | */
|
41 |
| -@TestMethodOrder(OrderAnnotation.class) |
42 | 38 | @ExtendWith(KubernetesClientExtension.class)
|
43 | 39 | class AuthIT {
|
44 | 40 |
|
45 | 41 | private static final String NAMESPACE = "default";
|
46 | 42 |
|
47 |
| - private static String secretName; |
48 |
| - private static String caData; |
49 |
| - private static String token; |
| 43 | + private static String nodeName; |
50 | 44 |
|
51 | 45 | @BeforeAll
|
52 |
| - static void setUp() { |
53 |
| - secretName = null; |
54 |
| - caData = null; |
55 |
| - token = null; |
56 |
| - } |
57 |
| - |
58 |
| - @Test |
59 |
| - @Order(1) |
60 |
| - void retrieveServiceAccount() throws IOException { |
61 |
| - // When |
62 |
| - final ServiceAccount sa = KC.create(CoreV1Api.class).listNamespacedServiceAccount(NAMESPACE) |
63 |
| - .stream().findFirst().orElse(null); |
64 |
| - // Then |
65 |
| - assertThat(sa).isNotNull(); |
66 |
| - secretName = sa.getSecrets().stream().findFirst().map(ObjectReference::getName).orElse(null); |
67 |
| - } |
68 |
| - |
69 |
| - @Test |
70 |
| - @Order(2) |
71 |
| - void retrieveSecretForServiceAccount() throws IOException { |
72 |
| - // When |
73 |
| - final Secret secret = KC.create(CoreV1Api.class).listNamespacedSecret(NAMESPACE) |
74 |
| - .stream() |
75 |
| - .filter(s -> s.getType().equals("kubernetes.io/service-account-token")) |
76 |
| - .filter(s -> s.getMetadata().getName().equals(secretName)) |
77 |
| - .findAny().orElse(null); |
78 |
| - // Then |
79 |
| - assertThat(secret).isNotNull(); |
80 |
| - assertThat(secret.getData()).containsKeys("ca.crt", "token"); |
81 |
| - caData = secret.getData().get("ca.crt"); |
82 |
| - token = secret.getData().get("token"); |
| 46 | + static void setUp() throws IOException { |
| 47 | + nodeName = KC.create(CoreV1Api.class).listNode().stream().findFirst() |
| 48 | + .orElseThrow(() -> new IllegalStateException("Node not accessible with default client")) |
| 49 | + .getMetadata().getName(); |
83 | 50 | }
|
84 | 51 |
|
85 | 52 | @Test
|
86 |
| - @Order(3) |
87 | 53 | void performTokenAuthInNewClient() throws IOException {
|
88 | 54 | // Given
|
| 55 | + final Secret secret = retrieveSecretForServiceAccount(retrieveServiceAccountSecret()); |
89 | 56 | final Configuration configuration = Configuration.builder()
|
90 | 57 | .server(KC.getConfiguration().getServer())
|
91 |
| - .certificateAuthorityData(caData) |
92 |
| - .token(token) |
| 58 | + .certificateAuthorityData(secret.getData().get("ca.crt")) |
| 59 | + .token(secret.getData().get("token")) |
93 | 60 | .build();
|
94 |
| - final KubernetesClient tokenClient = new KubernetesClient(configuration); |
95 |
| - final String accessibleTokenName = KC.create(CoreV1Api.class).listNode().stream().findFirst() |
96 |
| - .orElseThrow(() -> new IllegalStateException("Node not accessible with default client")) |
97 |
| - .getMetadata().getName(); |
98 | 61 | // When
|
99 |
| - final Node node = tokenClient.create(CoreV1Api.class).listNode().stream().findFirst() |
| 62 | + final Node node = new KubernetesClient(configuration).create(CoreV1Api.class) |
| 63 | + .listNode().stream().findFirst() |
100 | 64 | .orElse(null);
|
101 | 65 | // Then
|
102 |
| - assertThat(node).isNotNull(); |
103 |
| - assertThat(node.getMetadata().getName()).isEqualTo(accessibleTokenName); |
| 66 | + assertThat(node) |
| 67 | + .isNotNull() |
| 68 | + .hasFieldOrPropertyWithValue("metadata.name", nodeName); |
| 69 | + } |
| 70 | + |
| 71 | + private String retrieveServiceAccountSecret() throws IOException { |
| 72 | + return KC.create(CoreV1Api.class).listNamespacedServiceAccount(NAMESPACE) |
| 73 | + .stream().findFirst() |
| 74 | + .map(ServiceAccount::getSecrets) |
| 75 | + .flatMap(secrets -> secrets.stream().findFirst().map(ObjectReference::getName)) |
| 76 | + .orElseThrow(() -> new AssertionError("No Service Account found")); |
104 | 77 | }
|
| 78 | + |
| 79 | + private Secret retrieveSecretForServiceAccount(String secretName) throws IOException { |
| 80 | + return KC.create(CoreV1Api.class).listNamespacedSecret(NAMESPACE) |
| 81 | + .stream() |
| 82 | + .filter(s -> s.getType().equals("kubernetes.io/service-account-token")) |
| 83 | + .filter(s -> s.getMetadata().getName().equals(secretName)) |
| 84 | + .findAny() |
| 85 | + .orElseThrow(() -> new AssertionError(String.format("Secret %s doesn't exist", secretName))); |
| 86 | + } |
| 87 | + |
105 | 88 | }
|
0 commit comments