Skip to content

Commit 2105513

Browse files
author
John Daniels
committed
Correctly load base64-encoded strings from Secret Yaml files.
1 parent 0d991e1 commit 2105513

File tree

3 files changed

+46
-9
lines changed

3 files changed

+46
-9
lines changed

util/src/main/java/io/kubernetes/client/util/Yaml.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
import java.io.IOException;
2020
import java.io.Reader;
2121
import java.io.StringReader;
22-
import java.util.ArrayList;
23-
import java.util.HashMap;
24-
import java.util.List;
25-
import java.util.Map;
26-
import java.util.Set;
22+
import java.util.*;
23+
import okio.ByteString;
2724
import org.apache.commons.lang3.tuple.MutablePair;
2825
import org.apache.commons.lang3.tuple.Pair;
2926
import org.slf4j.Logger;
@@ -269,6 +266,9 @@ protected Object constructObject(Node node) {
269266
if (node.getType() == IntOrString.class) {
270267
return constructIntOrString((ScalarNode) node);
271268
}
269+
if (node.getType() == byte[].class) {
270+
return constructByteArray((ScalarNode) node);
271+
}
272272
return super.constructObject(node);
273273
}
274274

@@ -279,6 +279,10 @@ private IntOrString constructIntOrString(ScalarNode node) {
279279
return new IntOrString(node.getValue());
280280
}
281281
}
282+
283+
private byte[] constructByteArray(ScalarNode node) {
284+
return ByteString.decodeBase64(node.getValue()).toByteArray();
285+
}
282286
}
283287

284288
/** @return An instantiated SnakeYaml Object. */

util/src/test/java/io/kubernetes/client/util/YamlTest.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515
import static org.junit.Assert.*;
1616

1717
import com.google.common.io.Resources;
18-
import io.kubernetes.client.models.AppsV1beta1Deployment;
19-
import io.kubernetes.client.models.V1ObjectMeta;
20-
import io.kubernetes.client.models.V1Service;
21-
import io.kubernetes.client.models.V1ServicePort;
18+
import io.kubernetes.client.models.*;
2219
import java.io.File;
2320
import java.io.IOException;
2421
import java.io.StringReader;
2522
import java.lang.reflect.Method;
23+
import java.nio.charset.StandardCharsets;
2624
import java.util.List;
2725
import org.junit.Test;
2826

@@ -123,6 +121,13 @@ public void testLoadAllFile() throws Exception {
123121
assertEquals("apps/v1beta1", deploy.getApiVersion());
124122
assertEquals("Deployment", deploy.getKind());
125123
assertEquals("helloworld", deploy.getMetadata().getName());
124+
} else if (type.equals("V1Secret")) {
125+
V1Secret secret = (V1Secret) object;
126+
assertEquals("Secret", secret.getKind());
127+
assertEquals("secret", secret.getMetadata().getName());
128+
assertEquals("Opaque", secret.getType());
129+
assertEquals(
130+
"hello", new String(secret.getData().get("secret-data"), StandardCharsets.UTF_8));
126131
} else {
127132
throw new Exception("some thing wrong happened");
128133
}
@@ -149,4 +154,21 @@ public void testLoadIntOrString() {
149154
assertNull("Unexpected exception: " + ex.toString(), ex);
150155
}
151156
}
157+
158+
@Test
159+
public void testLoadBytes() {
160+
try {
161+
String strInput = "data:\n hello: aGVsbG8=";
162+
163+
V1Secret secret = Yaml.loadAs(strInput, V1Secret.class);
164+
165+
assertEquals(
166+
"Incorrect value loaded for Base64 encoded secret",
167+
"hello",
168+
new String(secret.getData().get("hello"), StandardCharsets.UTF_8));
169+
170+
} catch (Exception ex) {
171+
assertNull("Unexpected exception: " + ex.toString(), ex);
172+
}
173+
}
152174
}

util/src/test/resources/test.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,15 @@ spec:
3232
imagePullPolicy: Always
3333
args:
3434
- "-http=127.0.0.1:8080"
35+
---
36+
apiVersion: v1
37+
kind: Secret
38+
39+
metadata:
40+
name: secret
41+
42+
type: Opaque
43+
44+
data:
45+
secret-data: aGVsbG8=
3546

0 commit comments

Comments
 (0)