RunAsPlatformSpecificSetupBuilder implements CommandLineSetupBuilder { @NotNull @Override public Iterable<CommandLineSetup> build(@NotNull final CommandLineSetup commandLineSetup) { final UserCredentials userCredentials = myUserCredentialsService.tryGetUserCredentials(); if(userCredentials == null) { return Collections.singleton(commandLineSetup); } if(!myRunAsAccessService.getIsRunAsEnabled()) { throw new BuildStartException("RunAs is not enabled"); } final ArrayList<CommandLineResource> resources = new ArrayList<CommandLineResource>(); resources.addAll(commandLineSetup.getResources()); final File settingsFile = myFileService.getTempFileName(ARGS_EXT); resources.add(new CommandLineFile(myBeforeBuildPublisher, settingsFile.getAbsoluteFile(), myUserCredentialsGenerator.create(userCredentials))); List<CommandLineArgument> cmdLineArgs = new ArrayList<CommandLineArgument>(); cmdLineArgs.add(new CommandLineArgument(commandLineSetup.getToolPath(), CommandLineArgument.Type.PARAMETER)); cmdLineArgs.addAll(commandLineSetup.getArgs()); final RunAsParams params = new RunAsParams(cmdLineArgs); final File commandFile = myFileService.getTempFileName(myCommandFileExtension); resources.add(new CommandLineFile(myBeforeBuildPublisher, commandFile.getAbsoluteFile(), myRunAsCmdGenerator.create(params))); final ArrayList<AccessControlEntry> acl = new ArrayList<AccessControlEntry>(); for (AccessControlEntry ace: myAccessControlListProvider.getAcl(userCredentials)) { acl.add(ace); } acl.add(new AccessControlEntry(commandFile, AccessControlAccount.forUser(userCredentials.getUser()), EnumSet.of(AccessPermissions.GrantExecute), AccessControlScope.Step)); final File runAsToolPath = getTool(); final AccessControlEntry runAsToolAce = new AccessControlEntry(runAsToolPath, AccessControlAccount.forUser(userCredentials.getUser()), EnumSet.of(AccessPermissions.GrantExecute), AccessControlScope.Build); acl.add(runAsToolAce); myAccessControlResource.setAcl(new AccessControlList(acl)); resources.add(myAccessControlResource); final CommandLineSetup runAsCommandLineSetup = new CommandLineSetup( runAsToolPath.getAbsolutePath(), Arrays.asList( new CommandLineArgument(settingsFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER), new CommandLineArgument(commandFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER), new CommandLineArgument(myBuildAgentSystemInfo.bitness().toString(), CommandLineArgument.Type.PARAMETER), new CommandLineArgument(myArgumentConverter.convert(userCredentials.getPassword()), CommandLineArgument.Type.PARAMETER)), resources); myRunAsLogger.LogRunAs(userCredentials, commandLineSetup, runAsCommandLineSetup); return Collections.singleton(runAsCommandLineSetup); } RunAsPlatformSpecificSetupBuilder(
    @NotNull final UserCredentialsService userCredentialsService,
    @NotNull final RunnerParametersService runnerParametersService,
    @NotNull final FileService fileService,
    @NotNull final BuildAgentSystemInfo buildAgentSystemInfo,
    @NotNull final AccessControlListProvider accessControlListProvider,
    @NotNull final ResourcePublisher beforeBuildPublisher,
    @NotNull final AccessControlResource accessControlResource,
    @NotNull final ResourceGenerator<UserCredentials> userCredentialsGenerator,
    @NotNull final ResourceGenerator<RunAsParams> runAsCmdGenerator,
    @NotNull final FileAccessService fileAccessService,
    @NotNull final RunAsLogger runAsLogger,
    @NotNull final RunAsAccessService runAsAccessService,
    @NotNull final Converter<String, String> argumentConverter,
    @NotNull final String commandFileExtension); @NotNull @Override Iterable<CommandLineSetup> build(@NotNull final CommandLineSetup commandLineSetup);  }
@Test() public void shouldBuildSetup() throws IOException { final File credentialsFile = new File("credentials"); final File cmdFile = new File("command"); final String toolName = "my tool"; final String runAsToolPath = "runAsPath"; final File runAsTool = new File(runAsToolPath, RunAsPlatformSpecificSetupBuilder.TOOL_FILE_NAME + ".abc"); final String user = "nik"; final String password = "abc"; final AccessControlEntry someAce = new AccessControlEntry(new File("tools"), AccessControlAccount.forAll(), EnumSet.of(AccessPermissions.GrantExecute), AccessControlScope.Step); final List<CommandLineArgument> args = Arrays.asList(new CommandLineArgument("arg1", CommandLineArgument.Type.PARAMETER), new CommandLineArgument("arg2", CommandLineArgument.Type.PARAMETER)); final List<CommandLineResource> resources = Arrays.asList(myCommandLineResource1, myCommandLineResource2); final String credentialsContent = "credentials content"; final String cmdContent = "args content"; final CommandLineSetup commandLineSetup = new CommandLineSetup(toolName, args, resources); final List<CommandLineArgument> runAsParamsArgs = new ArrayList<CommandLineArgument>(args); runAsParamsArgs.add(0, new CommandLineArgument(toolName, CommandLineArgument.Type.PARAMETER)); final RunAsParams params = new RunAsParams(runAsParamsArgs); final List<CommandLineArgument> additionalArgs = Arrays.asList(new CommandLineArgument("arg1", CommandLineArgument.Type.PARAMETER), new CommandLineArgument("arg 2", CommandLineArgument.Type.PARAMETER)); final UserCredentials userCredentials = new UserCredentials("profile", user, password, WindowsIntegrityLevel.Auto, LoggingLevel.Off, additionalArgs); final AccessControlList stepAcl = new AccessControlList(Arrays.asList(someAce)); final CommandLineSetup runAsCommandLineSetup = new CommandLineSetup( runAsTool.getAbsolutePath(), Arrays.asList( new CommandLineArgument(credentialsFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER), new CommandLineArgument(cmdFile.getAbsolutePath(), CommandLineArgument.Type.PARAMETER), new CommandLineArgument("64", CommandLineArgument.Type.PARAMETER), new CommandLineArgument("'" + password + "'", CommandLineArgument.Type.PARAMETER)), Arrays.asList( myCommandLineResource1, myCommandLineResource2, new CommandLineFile(myBeforeBuildPublisher, credentialsFile.getAbsoluteFile(), credentialsContent), new CommandLineFile(myBeforeBuildPublisher, cmdFile.getAbsoluteFile(), cmdContent), myAccessControlResource)); myCtx.checking(new Expectations() {{ oneOf(myRunAsAccessService).getIsRunAsEnabled(); will(returnValue(true)); oneOf(myUserCredentialsService).tryGetUserCredentials(); will(returnValue(userCredentials)); oneOf(myAccessControlListProvider).getAcl(userCredentials); will(returnValue(stepAcl)); oneOf(myFileService).getTempFileName(RunAsPlatformSpecificSetupBuilder.ARGS_EXT); will(returnValue(credentialsFile)); oneOf(myBuildAgentSystemInfo).bitness(); will(returnValue(Bitness.BIT64)); oneOf(myFileService).getTempFileName(".abc"); will(returnValue(cmdFile)); oneOf(myCredentialsGenerator).create(with(userCredentials)); will(returnValue(credentialsContent)); oneOf(myArgsGenerator).create(params); will(returnValue(cmdContent)); oneOf(myRunnerParametersService).getToolPath(Constants.RUN_AS_TOOL_NAME); will(returnValue(runAsToolPath)); oneOf(myFileService).validatePath(runAsTool); never(myFileAccessService).setAccess(with(any(AccessControlList.class))); oneOf(myAccessControlResource).setAcl( new AccessControlList(Arrays.asList( someAce, new AccessControlEntry(cmdFile, AccessControlAccount.forUser(user), EnumSet.of(AccessPermissions.GrantExecute), AccessControlScope.Step), new AccessControlEntry(runAsTool, AccessControlAccount.forUser(user), EnumSet.of(AccessPermissions.GrantExecute), AccessControlScope.Build)))); oneOf(myRunAsLogger).LogRunAs(userCredentials, commandLineSetup, runAsCommandLineSetup); allowing(myArgumentConverter).convert(with(any(String.class))); will(new CustomAction("convert") { @Override public Object invoke(final Invocation invocation) throws Throwable { return "'" + invocation.getParameter(0) + "'"; } }); }}); final CommandLineSetupBuilder instance = createInstance(); final CommandLineSetup setup = instance.build(commandLineSetup).iterator().next(); myCtx.assertIsSatisfied(); then(setup).isEqualTo(runAsCommandLineSetup); }
@Test() public void shouldNotBuildSetupWhenHaveNoSettings() throws IOException { final String toolName = "tool"; final List<CommandLineArgument> args = Arrays.asList(new CommandLineArgument("arg1", CommandLineArgument.Type.PARAMETER), new CommandLineArgument("arg2", CommandLineArgument.Type.PARAMETER)); final List<CommandLineResource> resources = Arrays.asList(myCommandLineResource1, myCommandLineResource2); final CommandLineSetup baseSetup = new CommandLineSetup(toolName, args, resources); myCtx.checking(new Expectations() {{ oneOf(myUserCredentialsService).tryGetUserCredentials(); will(returnValue(null)); }}); final CommandLineSetupBuilder instance = createInstance(); final CommandLineSetup setup = instance.build(new CommandLineSetup(toolName, args, resources)).iterator().next(); myCtx.assertIsSatisfied(); then(setup).isEqualTo(baseSetup); }
@Test() public void shouldThrowBuildStartExceptionWhenRunAsIsNotEnabled() throws IOException { final String user = "nik"; final String password = "abc"; final List<CommandLineArgument> additionalArgs = Arrays.asList(new CommandLineArgument("arg1", CommandLineArgument.Type.PARAMETER)); final UserCredentials userCredentials = new UserCredentials("profile", user, password, WindowsIntegrityLevel.Auto, LoggingLevel.Off, additionalArgs); final String toolName = "tool"; final List<CommandLineArgument> args = Arrays.asList(new CommandLineArgument("arg1", CommandLineArgument.Type.PARAMETER), new CommandLineArgument("arg2", CommandLineArgument.Type.PARAMETER)); final List<CommandLineResource> resources = Arrays.asList(myCommandLineResource1, myCommandLineResource2); myCtx.checking(new Expectations() {{ oneOf(myUserCredentialsService).tryGetUserCredentials(); will(returnValue(userCredentials)); oneOf(myRunAsAccessService).getIsRunAsEnabled(); will(returnValue(false)); }}); final CommandLineSetupBuilder instance = createInstance(); BuildStartException actualException = null; try { instance.build(new CommandLineSetup(toolName, args, resources)).iterator().next(); } catch (BuildStartException ex) { actualException = ex; } myCtx.assertIsSatisfied(); then(actualException).isNotNull(); }