Skip to content

Commit 8dfe406

Browse files
bluesliverxsaville
and
saville
authored
Raise exception when access is denied instead of silently failing (#257)
Co-authored-by: saville <[email protected]>
1 parent 44fea4f commit 8dfe406

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

src/main/java/com/datapipe/jenkins/vault/VaultAccessor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,8 @@ public static boolean responseHasErrors(VaultConfiguration configuration, PrintS
227227
}
228228
int status = restResponse.getStatus();
229229
if (status == 403) {
230-
logger.printf("Access denied to Vault Secrets at '%s'%n", path);
231-
return true;
230+
throw new VaultPluginException(
231+
String.format("Access denied to Vault path '%s'", path));
232232
} else if (status == 404) {
233233
if (configuration.getFailIfNotFound()) {
234234
throw new VaultPluginException(

src/test/java/com/datapipe/jenkins/vault/VaultBuildWrapperTest.java

+43-13
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ public class VaultBuildWrapperTest {
3838
@Test
3939
public void testWithNonExistingPath() throws IOException, InterruptedException {
4040
String path = "not/existing";
41-
TestWrapper wrapper = new TestWrapper(standardSecrets(path));
41+
VaultAccessor mockAccessor = mock(VaultAccessor.class);
42+
doReturn(mockAccessor).when(mockAccessor).init();
43+
LogicalResponse response = getNotFoundResponse();
44+
when(mockAccessor.read(path, 2)).thenReturn(response);
45+
TestWrapper wrapper = new TestWrapper(standardSecrets(path), mockAccessor);
4246
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
4347
PrintStream logger = new PrintStream(baos);
4448
SimpleBuildWrapper.Context context = null;
@@ -56,11 +60,38 @@ public void testWithNonExistingPath() throws IOException, InterruptedException {
5660
assertThat(e.getMessage(), is("Vault credentials not found for 'not/existing'"));
5761
}
5862

59-
wrapper.verifyCalls();
63+
verify(mockAccessor, times(2)).init();
64+
verify(mockAccessor, times(2)).read(path, 2);
6065
assertThat(new String(baos.toByteArray(), StandardCharsets.UTF_8),
6166
containsString("Vault credentials not found for 'not/existing'"));
6267
}
6368

69+
@Test
70+
public void testWithAccessDeniedPath() throws IOException, InterruptedException {
71+
String path = "not/allowed";
72+
VaultAccessor mockAccessor = mock(VaultAccessor.class);
73+
doReturn(mockAccessor).when(mockAccessor).init();
74+
LogicalResponse response = getAccessDeniedResponse();
75+
when(mockAccessor.read(path, 2)).thenReturn(response);
76+
TestWrapper wrapper = new TestWrapper(standardSecrets(path), mockAccessor);
77+
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
78+
PrintStream logger = new PrintStream(baos);
79+
SimpleBuildWrapper.Context context = null;
80+
Run<?, ?> build = mock(Build.class);
81+
when(build.getParent()).thenReturn(null);
82+
EnvVars envVars = mock(EnvVars.class);
83+
when(envVars.expand(path)).thenReturn(path);
84+
85+
try {
86+
wrapper.run(context, build, envVars, logger);
87+
} catch (VaultPluginException e) {
88+
assertThat(e.getMessage(), is("Access denied to Vault path 'not/allowed'"));
89+
}
90+
91+
verify(mockAccessor).init();
92+
verify(mockAccessor).read(path, 2);
93+
}
94+
6495
private List<VaultSecret> standardSecrets(String path) {
6596
List<VaultSecret> secrets = new ArrayList<>();
6697
VaultSecretValue secretValue = new VaultSecretValue("envVar1", "key1");
@@ -81,21 +112,25 @@ private LogicalResponse getNotFoundResponse() {
81112
return resp;
82113
}
83114

115+
private LogicalResponse getAccessDeniedResponse() {
116+
LogicalResponse resp = mock(LogicalResponse.class);
117+
RestResponse rest = mock(RestResponse.class);
118+
when(resp.getData()).thenReturn(new HashMap<>());
119+
when(resp.getRestResponse()).thenReturn(rest);
120+
when(rest.getStatus()).thenReturn(403);
121+
return resp;
122+
}
123+
84124
class TestWrapper extends VaultBuildWrapper {
85125

86-
VaultAccessor mockAccessor;
87126
VaultConfiguration vaultConfig = new VaultConfiguration();
88127

89-
public TestWrapper(List<VaultSecret> vaultSecrets) {
128+
public TestWrapper(List<VaultSecret> vaultSecrets, VaultAccessor mockAccessor) {
90129
super(vaultSecrets);
91130

92131
vaultConfig.setVaultUrl("testmock");
93132
vaultConfig.setVaultCredentialId("credId");
94133
vaultConfig.setFailIfNotFound(false);
95-
mockAccessor = mock(VaultAccessor.class);
96-
doReturn(mockAccessor).when(mockAccessor).init();
97-
LogicalResponse response = getNotFoundResponse();
98-
when(mockAccessor.read("not/existing", 2)).thenReturn(response);
99134
setVaultAccessor(mockAccessor);
100135
setConfiguration(vaultConfig);
101136
}
@@ -104,10 +139,5 @@ public void run(Context context, Run build, EnvVars envVars, PrintStream logger)
104139
this.logger = logger;
105140
provideEnvironmentVariablesFromVault(context, build, envVars);
106141
}
107-
108-
public void verifyCalls() {
109-
verify(mockAccessor, times(2)).init();
110-
verify(mockAccessor, times(2)).read("not/existing", 2);
111-
}
112142
}
113143
}

0 commit comments

Comments
 (0)