-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Tim Jacomb <timjacomb1@gmail.com> Co-authored-by: Yaroslav Afenkin <yaroslavafenkin@users.noreply.github.com>
- Loading branch information
1 parent
887e0f9
commit 64da817
Showing
13 changed files
with
285 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/test/java/com/microsoft/azure/util/AzureImdsCredentialsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.microsoft.azure.util; | ||
|
||
import com.cloudbees.hudson.plugins.folder.Folder; | ||
import hudson.model.Item; | ||
import hudson.model.User; | ||
import hudson.security.ACL; | ||
import hudson.security.ACLContext; | ||
import hudson.security.AccessDeniedException3; | ||
import hudson.util.FormValidation; | ||
import jenkins.model.Jenkins; | ||
import org.junit.Assert; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.jvnet.hudson.test.JenkinsRule; | ||
import org.jvnet.hudson.test.MockAuthorizationStrategy; | ||
|
||
public class AzureImdsCredentialsTest { | ||
|
||
@Rule | ||
public JenkinsRule j = new JenkinsRule(); | ||
|
||
@Test | ||
public void descriptorVerifyConfigurationAsAdmin() { | ||
// No security realm, anonymous has Overall/Administer | ||
final AzureImdsCredentials.DescriptorImpl descriptor = new AzureImdsCredentials.DescriptorImpl(); | ||
|
||
FormValidation result = descriptor.doVerifyConfiguration(null,"", "", ""); | ||
Assert.assertEquals(FormValidation.Kind.ERROR, result.kind); | ||
} | ||
|
||
@Test | ||
public void descriptorVerifyConfigurationWithAncestorAsAuthorizedUser() throws Exception { | ||
Folder folder = j.jenkins.createProject(Folder.class, "folder"); | ||
j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); | ||
MockAuthorizationStrategy authorizationStrategy = new MockAuthorizationStrategy(); | ||
authorizationStrategy.grant(Jenkins.READ).everywhere().to("user"); | ||
authorizationStrategy.grant(Item.CONFIGURE).onFolders(folder).to("user"); | ||
j.jenkins.setAuthorizationStrategy(authorizationStrategy); | ||
|
||
final AzureImdsCredentials.DescriptorImpl descriptor = new AzureImdsCredentials.DescriptorImpl(); | ||
|
||
try (ACLContext ctx = ACL.as(User.getOrCreateByIdOrFullName("user"))) { | ||
FormValidation result = descriptor.doVerifyConfiguration(folder, "", "", ""); | ||
// we aren't looking up an actual secret so this fails with missing protocol | ||
// TODO mock secrets retrieval so we can test the happy case here properly | ||
Assert.assertEquals(FormValidation.Kind.ERROR, result.kind); | ||
} | ||
} | ||
|
||
@Test | ||
public void descriptorVerifyConfigurationWithAncestorAsUnauthorizedUser() throws Exception { | ||
Folder folder = j.jenkins.createProject(Folder.class, "folder"); | ||
j.jenkins.setSecurityRealm(j.createDummySecurityRealm()); | ||
MockAuthorizationStrategy authorizationStrategy = new MockAuthorizationStrategy(); | ||
authorizationStrategy.grant(Jenkins.READ).everywhere().to("user"); | ||
j.jenkins.setAuthorizationStrategy(authorizationStrategy); | ||
|
||
final AzureImdsCredentials.DescriptorImpl descriptor = new AzureImdsCredentials.DescriptorImpl(); | ||
|
||
try (ACLContext ctx = ACL.as(User.getOrCreateByIdOrFullName("user"))) { | ||
Assert.assertThrows(AccessDeniedException3.class, () -> descriptor.doVerifyConfiguration(folder, "", "", "")); | ||
} | ||
} | ||
} |
Oops, something went wrong.