-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
KubernetesMutationHookHandlingTest.java
223 lines (193 loc) · 8.94 KB
/
KubernetesMutationHookHandlingTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/*
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubeapitest.sample;
import io.fabric8.kubeapitest.cert.CertManager;
import io.fabric8.kubeapitest.junit.EnableKubeAPIServer;
import io.fabric8.kubeapitest.junit.KubeConfig;
import io.fabric8.kubernetes.api.model.admission.v1.AdmissionReview;
import io.fabric8.kubernetes.api.model.admissionregistration.v1.MutatingWebhookConfiguration;
import io.fabric8.kubernetes.api.model.networking.v1.*;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.KubernetesClientBuilder;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.javaoperatorsdk.webhook.admission.AdmissionController;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.bouncycastle.asn1.x509.GeneralName;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.AbstractHandler;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Base64;
import java.util.HashMap;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Test demonstrates how to test locally Mutating Webhooks, in real like you implementation such an
* endpoint would be simpler, using frameworks like
* <a href="https://github.com/java-operator-sdk/kubernetes-webooks-framework">Kubernetes Webhook
* Framework</a> with combination of Quarkus or Spring.
*/
@EnableKubeAPIServer
class KubernetesMutationHookHandlingTest {
@KubeConfig
static String kubeConfig;
private static final Logger log = LoggerFactory.getLogger(KubernetesMutationHookHandlingTest.class);
public static final String PASSWORD = "secret";
public static final String TEST_LABEL_KEY = "test-label";
public static final String TEST_LABEL_VALUE = "mutation-test";
static File certFile = new File("target", "mutation.crt");
// server that handles mutation hooks
static Server server = new Server();
// using https://github.com/java-operator-sdk/kubernetes-webooks-framework framework to implement
// the response
static AdmissionController<Ingress> mutationController = new AdmissionController<>((resource, operation) -> {
if (resource.getMetadata().getLabels() == null) {
resource.getMetadata().setLabels(new HashMap<>());
}
resource.getMetadata().getLabels().putIfAbsent(TEST_LABEL_KEY, TEST_LABEL_VALUE);
return resource;
});
@Test
void handleMutatingWebhook() {
var client = new KubernetesClientBuilder().withConfig(Config.fromKubeconfig(kubeConfig)).build();
applyConfig(client);
var ingress = client.resource(testIngress()).create();
assertThat(ingress.getMetadata().getLabels()).containsEntry(TEST_LABEL_KEY, TEST_LABEL_VALUE);
}
@BeforeAll
static void startWebhookServer() throws Exception {
initServerConfigs();
server.setHandler(new AbstractHandler() {
@Override
public void handle(String s, Request request, HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
try {
request.setHandled(true);
AdmissionReview admissionReview = Serialization.unmarshal(httpServletRequest.getInputStream());
var response = mutationController.handle(admissionReview);
var out = httpServletResponse.getWriter();
httpServletResponse.setContentType("application/json");
httpServletResponse.setCharacterEncoding(StandardCharsets.UTF_8.toString());
httpServletResponse.setStatus(HttpServletResponse.SC_OK);
out.println(Serialization.asJson(response));
} catch (Exception e) {
log.error("Error in webhook", e);
throw new RuntimeException(e);
}
}
});
server.start();
}
@AfterAll
static void stopWebhookServer() throws Exception {
server.stop();
}
private void applyConfig(KubernetesClient client) {
try (var resource = KubernetesMutationHookHandlingTest.class
.getResourceAsStream("/MutatingWebhookConfig.yaml")) {
MutatingWebhookConfiguration hook = (MutatingWebhookConfiguration) client.load(resource).items().get(0);
String cert = Files.readString(certFile.toPath(), StandardCharsets.UTF_8);
hook.getWebhooks().get(0).getClientConfig()
.setCaBundle(new String(Base64.getEncoder().encode(cert.getBytes())));
client.resource(hook).create();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private static void initServerConfigs() {
HttpConfiguration httpConfig = new HttpConfiguration();
httpConfig.addCustomizer(new SecureRequestCustomizer());
HttpConnectionFactory http11 = new HttpConnectionFactory(httpConfig);
SslContextFactory.Server sslContextFactory = new SslContextFactory.Server();
var keyFile = new File("target", "mutation.key");
// certificates are generated in pem format, since are used also as an input for
// MutatingWebhookConfiguration CA Bundle
CertManager.generateKeyAndCertificate("CN=example.org", keyFile, certFile,
new GeneralName(GeneralName.iPAddress, "127.0.0.1"));
sslContextFactory.setKeyStore(createKeyStoreFrom(keyFile, certFile));
sslContextFactory.setKeyStorePassword(PASSWORD);
SslConnectionFactory tls = new SslConnectionFactory(sslContextFactory, http11.getProtocol());
ServerConnector connector = new ServerConnector(server, tls, http11);
connector.setPort(8443);
server.addConnector(connector);
}
/** Creates Keystore from generated Certificate/Key Pem files */
private static KeyStore createKeyStoreFrom(File keyPem, File certPem) {
try (var certReader = new PEMParser(new InputStreamReader(new FileInputStream(certPem)));
var keyReader = new PEMParser(new InputStreamReader(new FileInputStream(keyPem)))) {
var certConverter = new JcaX509CertificateConverter();
X509Certificate cert = certConverter.getCertificate((X509CertificateHolder) certReader.readObject());
JcaPEMKeyConverter keyConverter = new JcaPEMKeyConverter();
PrivateKey key = keyConverter.getPrivateKey(((PEMKeyPair) keyReader.readObject()).getPrivateKeyInfo());
KeyStore keystore = KeyStore.getInstance("JKS");
keystore.load(null);
keystore.setCertificateEntry("cert-alias", cert);
keystore.setKeyEntry("key-alias", key, PASSWORD.toCharArray(), new Certificate[] { cert });
return keystore;
} catch (IOException | CertificateException | KeyStoreException | NoSuchAlgorithmException e) {
throw new RuntimeException(e);
}
}
public static Ingress testIngress() {
return new IngressBuilder()
.withNewMetadata()
.withName("test1")
.endMetadata()
.withSpec(new IngressSpecBuilder()
.withIngressClassName("sample")
.withRules(new IngressRuleBuilder()
.withHttp(new HTTPIngressRuleValueBuilder()
.withPaths(new HTTPIngressPathBuilder()
.withPath("/test")
.withPathType("Prefix")
.withBackend(new IngressBackendBuilder()
.withService(new IngressServiceBackendBuilder()
.withName("service")
.withPort(new ServiceBackendPortBuilder()
.withNumber(80)
.build())
.build())
.build())
.build())
.build())
.build())
.build())
.build();
}
}