@Issue("JENKINS-37871") @Test public void secretBuildWrapperRunsBeforeNormalWrapper() throws Exception { StringCredentialsImpl firstCreds = new StringCredentialsImpl(CredentialsScope.GLOBAL, credentialsId, "sample1", Secret.fromString(password)); CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), firstCreds); SecretBuildWrapper wrapper = new SecretBuildWrapper(Arrays.asList(new StringBinding(bindingKey, credentialsId))); FreeStyleProject f = r.createFreeStyleProject("buildWrapperOrder"); f.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo %PASS_1%") : new Shell("echo $PASS_1")); f.getBuildWrappersList().add(new BuildWrapperOrder()); f.getBuildWrappersList().add(wrapper); // configRoundtrip makes sure the ordinal of SecretBuildWrapper extension is applied correctly. r.configRoundtrip(f); FreeStyleBuild b = r.buildAndAssertSuccess(f); r.assertLogContains("Secret found!", b); }
@Test public void basics() throws Exception { String username = "bob"; String password = "s3cr3t"; UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, "sample", username, password); CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c); FreeStyleProject p = r.createFreeStyleProject(); p.getBuildWrappersList().add(new SecretBuildWrapper(Collections.<Binding<?>>singletonList(new UsernamePasswordBinding("AUTH", c.getId())))); p.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo %AUTH% > auth.txt") : new Shell("echo $AUTH > auth.txt")); r.configRoundtrip(p); SecretBuildWrapper wrapper = p.getBuildWrappersList().get(SecretBuildWrapper.class); assertNotNull(wrapper); List<? extends MultiBinding<?>> bindings = wrapper.getBindings(); assertEquals(1, bindings.size()); MultiBinding<?> binding = bindings.get(0); assertEquals(c.getId(), binding.getCredentialsId()); assertEquals(UsernamePasswordBinding.class, binding.getClass()); assertEquals("AUTH", ((UsernamePasswordBinding) binding).getVariable()); FreeStyleBuild b = r.buildAndAssertSuccess(p); r.assertLogNotContains(password, b); assertEquals(username + ':' + password, b.getWorkspace().child("auth.txt").readToString().trim()); assertEquals("[AUTH]", b.getSensitiveBuildVariables().toString()); }
/** * this is an integration test as the bind is a bit tricky to mock out * @throws Exception */ @Test public void bindAddsNoNewVariablesAndValuesYet() throws Exception{ AwsBucketCredentialsImpl credentials = mock(AwsBucketCredentialsImpl.class); when(credentials.getId()).thenReturn("id"); when(credentials.getPassword()).thenReturn(Secret.fromString("password")); when(credentials.getUsername()).thenReturn("username"); CredentialsProvider.lookupStores(jenkinsRule.jenkins).iterator().next().addCredentials(Domain.global(), credentials); FreeStyleProject p = jenkinsRule.createFreeStyleProject(); p.getBuildWrappersList().add(new SecretBuildWrapper(Collections.<MultiBinding<?>>singletonList(new AwsBucketCredentialsBinding("userid", "pass", "id")))); if (Functions.isWindows()) { p.getBuildersList().add(new BatchFile("@echo off\necho %userid%/%pass% > auth.txt")); } else { p.getBuildersList().add(new Shell("set +x\necho $userid/$pass > auth.txt")); } jenkinsRule.configRoundtrip((Item)p); SecretBuildWrapper wrapper = p.getBuildWrappersList().get(SecretBuildWrapper.class); assertThat(wrapper).isNotNull(); List<? extends MultiBinding<?>> bindings = wrapper.getBindings(); assertThat(bindings).hasSize(1); MultiBinding<?> binding = bindings.get(0); assertThat(((AwsBucketCredentialsBinding) binding).getUsernameVariable()).isEqualTo("userid"); assertThat(((AwsBucketCredentialsBinding) binding).getPasswordVariable()).isEqualTo("pass"); FreeStyleBuild b = jenkinsRule.buildAndAssertSuccess(p); assertThat(b.getWorkspace().child("auth.txt").readToString().trim()).contains("username/password"); }
/** * Return correct CommandInterpreter based on OS * * @param launcher * @param script * @return CommandInterpreter */ private CommandInterpreter getCommandInterpreter(final Launcher launcher, final String script) { if (launcher.isUnix()) return new Shell(script); else return new BatchFile(script); }
private boolean dumpBuildSteps( List<Builder> builders, List<File> generatedScripts, File srcDir ) throws IOException { boolean isLinux = true; int i = 0; for (Builder builder : builders) { if (!(builder instanceof CommandInterpreter)) { continue; } CommandInterpreter ci = (CommandInterpreter) builder; File builderScript; if (builder instanceof BatchFile) { isLinux = false; builderScript = new File(srcDir, String.format("step_%d.bat", i++)); } else { builderScript = new File(srcDir, String.format("step_%d.sh", i++)); } BufferedWriter out = new BufferedWriter( new FileWriter(builderScript, true) ); String cmd = ci.getCommand(); if (!isLinux) { //Ensure windows line-endings cmd = cmd.replaceAll("\r\n", "\n").replaceAll("\n", "\r\n"); } out.write(cmd); builderScript.setExecutable(true, false); builderScript.setReadable(true, false); builderScript.setWritable(true, false); out.close(); generatedScripts.add(builderScript); } return isLinux; }
@Test public void basics() throws Exception { String username = "bob"; String password = "s3cr3t"; UsernamePasswordCredentialsImpl c = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, null, "sample", username, password); CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c); FreeStyleProject p = r.createFreeStyleProject(); p.getBuildWrappersList().add(new SecretBuildWrapper(Collections.<MultiBinding<?>>singletonList(new UsernamePasswordMultiBinding("userid", "pass", c.getId())))); if (Functions.isWindows()) { p.getBuildersList().add(new BatchFile("@echo off\necho %userid%/%pass% > auth.txt")); } else { p.getBuildersList().add(new Shell("set +x\necho $userid/$pass > auth.txt")); } r.configRoundtrip((Item)p); SecretBuildWrapper wrapper = p.getBuildWrappersList().get(SecretBuildWrapper.class); assertNotNull(wrapper); List<? extends MultiBinding<?>> bindings = wrapper.getBindings(); assertEquals(1, bindings.size()); MultiBinding<?> binding = bindings.get(0); assertEquals(c.getId(), binding.getCredentialsId()); assertEquals(UsernamePasswordMultiBinding.class, binding.getClass()); assertEquals("userid", ((UsernamePasswordMultiBinding) binding).getUsernameVariable()); assertEquals("pass", ((UsernamePasswordMultiBinding) binding).getPasswordVariable()); FreeStyleBuild b = r.buildAndAssertSuccess(p); r.assertLogNotContains(password, b); assertEquals(username + '/' + password, b.getWorkspace().child("auth.txt").readToString().trim()); assertEquals("[pass, userid]", new TreeSet<String>(b.getSensitiveBuildVariables()).toString()); }
@Issue("JENKINS-24805") @Test public void maskingFreeStyleSecrets() throws Exception { String firstCredentialsId = "creds_1"; String firstPassword = "p4$$"; StringCredentialsImpl firstCreds = new StringCredentialsImpl(CredentialsScope.GLOBAL, firstCredentialsId, "sample1", Secret.fromString(firstPassword)); CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), firstCreds); String secondCredentialsId = "creds_2"; String secondPassword = "p4$$" + "someMoreStuff"; StringCredentialsImpl secondCreds = new StringCredentialsImpl(CredentialsScope.GLOBAL, secondCredentialsId, "sample2", Secret.fromString(secondPassword)); CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), secondCreds); SecretBuildWrapper wrapper = new SecretBuildWrapper(Arrays.asList(new StringBinding("PASS_1", firstCredentialsId), new StringBinding("PASS_2", secondCredentialsId))); FreeStyleProject f = r.createFreeStyleProject(); f.setConcurrentBuild(true); f.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo %PASS_1%") : new Shell("echo \"$PASS_1\"")); f.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo %PASS_2%") : new Shell("echo \"$PASS_2\"")); f.getBuildWrappersList().add(wrapper); r.configRoundtrip((Item)f); FreeStyleBuild b = r.buildAndAssertSuccess(f); r.assertLogNotContains(firstPassword, b); r.assertLogNotContains(secondPassword, b); r.assertLogContains("****", b); }
@Issue("JENKINS-24805") @Test public void emptySecretsList() throws Exception { SecretBuildWrapper wrapper = new SecretBuildWrapper(new ArrayList<MultiBinding<?>>()); FreeStyleProject f = r.createFreeStyleProject(); f.setConcurrentBuild(true); f.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo PASSES") : new Shell("echo PASSES")); f.getBuildWrappersList().add(wrapper); r.configRoundtrip((Item)f); FreeStyleBuild b = r.buildAndAssertSuccess(f); r.assertLogContains("PASSES", b); }
@Issue("JENKINS-41760") @Test public void emptySecret() throws Exception { CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), new StringCredentialsImpl(CredentialsScope.GLOBAL, "creds", null, Secret.fromString(""))); FreeStyleProject p = r.createFreeStyleProject(); p.getBuildWrappersList().add(new SecretBuildWrapper(Collections.singletonList(new StringBinding("SECRET", "creds")))); p.getBuildersList().add(Functions.isWindows() ? new BatchFile("echo PASSES") : new Shell("echo PASSES")); r.assertLogContains("PASSES", r.buildAndAssertSuccess(p)); }
@Test public void basics() throws Exception { String alias = "androiddebugkey"; String password = "android"; StandardCertificateCredentials c = new CertificateCredentialsImpl(CredentialsScope.GLOBAL, null, alias, password, new CertificateCredentialsImpl.FileOnMasterKeyStoreSource(certificate.getAbsolutePath())); CredentialsProvider.lookupStores(r.jenkins).iterator().next().addCredentials(Domain.global(), c); FreeStyleProject p = r.createFreeStyleProject(); CertificateMultiBinding binding = new CertificateMultiBinding("keystore", c.getId()); binding.setAliasVariable("alias"); binding.setPasswordVariable("password"); p.getBuildWrappersList().add(new SecretBuildWrapper(Collections .<MultiBinding<?>> singletonList(binding))); if (Functions.isWindows()) { p.getBuildersList().add(new BatchFile( "echo | set /p=\"%alias%/%password%/\" > secrets.txt\r\n" + "IF EXIST %keystore% (\r\n" + "echo | set /p=\"exists\" >> secrets.txt\r\n" + ") ELSE (\r\n" + "echo | set /p=\"missing\" >> secrets.txt\r\n" + ")\r\n" + "exit 0")); } else { p.getBuildersList().add(new Shell( "printf $alias/$password/ > secrets.txt\n" + "if [ -f \"$keystore\" ]\n" + "then\n" + "printf exists >> secrets.txt\n" + "else\n" + "printf missing >> secrets.txt\n" + "fi")); } r.configRoundtrip((Item) p); SecretBuildWrapper wrapper = p.getBuildWrappersList().get(SecretBuildWrapper.class); assertNotNull(wrapper); List<? extends MultiBinding<?>> bindings = wrapper.getBindings(); assertEquals(1, bindings.size()); MultiBinding<?> testBinding = bindings.get(0); assertEquals(c.getId(), testBinding.getCredentialsId()); assertEquals(CertificateMultiBinding.class, testBinding.getClass()); assertEquals("password", ((CertificateMultiBinding) testBinding).getPasswordVariable()); assertEquals("alias", ((CertificateMultiBinding) testBinding).getAliasVariable()); assertEquals("keystore", ((CertificateMultiBinding) testBinding).getKeystoreVariable()); FreeStyleBuild b = r.buildAndAssertSuccess(p); r.assertLogNotContains(password, b); assertEquals(alias + '/' + password + "/exists", b.getWorkspace().child("secrets.txt").readToString().trim()); assertThat(b.getSensitiveBuildVariables(), containsInAnyOrder("keystore", "password", "alias")); }