@@ -38,7 +38,11 @@ public class VaultBuildWrapperTest {
38
38
@ Test
39
39
public void testWithNonExistingPath () throws IOException , InterruptedException {
40
40
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 );
42
46
final ByteArrayOutputStream baos = new ByteArrayOutputStream ();
43
47
PrintStream logger = new PrintStream (baos );
44
48
SimpleBuildWrapper .Context context = null ;
@@ -56,11 +60,38 @@ public void testWithNonExistingPath() throws IOException, InterruptedException {
56
60
assertThat (e .getMessage (), is ("Vault credentials not found for 'not/existing'" ));
57
61
}
58
62
59
- wrapper .verifyCalls ();
63
+ verify (mockAccessor , times (2 )).init ();
64
+ verify (mockAccessor , times (2 )).read (path , 2 );
60
65
assertThat (new String (baos .toByteArray (), StandardCharsets .UTF_8 ),
61
66
containsString ("Vault credentials not found for 'not/existing'" ));
62
67
}
63
68
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
+
64
95
private List <VaultSecret > standardSecrets (String path ) {
65
96
List <VaultSecret > secrets = new ArrayList <>();
66
97
VaultSecretValue secretValue = new VaultSecretValue ("envVar1" , "key1" );
@@ -81,21 +112,25 @@ private LogicalResponse getNotFoundResponse() {
81
112
return resp ;
82
113
}
83
114
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
+
84
124
class TestWrapper extends VaultBuildWrapper {
85
125
86
- VaultAccessor mockAccessor ;
87
126
VaultConfiguration vaultConfig = new VaultConfiguration ();
88
127
89
- public TestWrapper (List <VaultSecret > vaultSecrets ) {
128
+ public TestWrapper (List <VaultSecret > vaultSecrets , VaultAccessor mockAccessor ) {
90
129
super (vaultSecrets );
91
130
92
131
vaultConfig .setVaultUrl ("testmock" );
93
132
vaultConfig .setVaultCredentialId ("credId" );
94
133
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 );
99
134
setVaultAccessor (mockAccessor );
100
135
setConfiguration (vaultConfig );
101
136
}
@@ -104,10 +139,5 @@ public void run(Context context, Run build, EnvVars envVars, PrintStream logger)
104
139
this .logger = logger ;
105
140
provideEnvironmentVariablesFromVault (context , build , envVars );
106
141
}
107
-
108
- public void verifyCalls () {
109
- verify (mockAccessor , times (2 )).init ();
110
- verify (mockAccessor , times (2 )).read ("not/existing" , 2 );
111
- }
112
142
}
113
143
}
0 commit comments