Search in sources :

Example 1 with ValidatorResult

use of com.searchcode.app.model.ValidatorResult in project searchcode-server by boyter.

the class ApiRouteService method repoAdd.

public ApiResponse repoAdd(Request request, Response response) {
    if (!this.apiEnabled) {
        return new ApiResponse(false, "API not enabled");
    }
    Optional<String> publicKey = Optional.ofNullable(request.queryParams(Values.PUB));
    Optional<String> signedKey = Optional.ofNullable(request.queryParams(Values.SIG));
    Optional<String> reponames = Optional.ofNullable(request.queryParams(Values.REPONAME));
    Optional<String> repourls = Optional.ofNullable(request.queryParams("repourl"));
    Optional<String> repotype = Optional.ofNullable(request.queryParams("repotype"));
    Optional<String> repousername = Optional.ofNullable(request.queryParams("repousername"));
    Optional<String> repopassword = Optional.ofNullable(request.queryParams("repopassword"));
    Optional<String> reposource = Optional.ofNullable(request.queryParams("reposource"));
    Optional<String> repobranch = Optional.ofNullable(request.queryParams("repobranch"));
    Optional<String> hmacTypeString = Optional.ofNullable(request.queryParams(Values.HMAC));
    // Optional
    Optional<String> source = Optional.ofNullable(request.queryParams("source"));
    Optional<String> sourceuser = Optional.ofNullable(request.queryParams("sourceuser"));
    Optional<String> sourceproject = Optional.ofNullable(request.queryParams("sourceproject"));
    if (this.helpers.isNullEmptyOrWhitespace(reponames.orElse(Values.EMPTYSTRING))) {
        return new ApiResponse(false, "reponame is a required parameter");
    }
    if (this.helpers.isNullEmptyOrWhitespace(repourls.orElse(Values.EMPTYSTRING))) {
        return new ApiResponse(false, "repourl is a required parameter");
    }
    if (!repotype.isPresent()) {
        return new ApiResponse(false, "repotype is a required parameter");
    }
    if (!repousername.isPresent()) {
        return new ApiResponse(false, "repousername is a required parameter");
    }
    if (!repopassword.isPresent()) {
        return new ApiResponse(false, "repopassword is a required parameter");
    }
    if (!reposource.isPresent()) {
        return new ApiResponse(false, "reposource is a required parameter");
    }
    if (!repobranch.isPresent()) {
        return new ApiResponse(false, "repobranch is a required parameter");
    }
    if (this.apiAuth) {
        if (!publicKey.isPresent() || this.helpers.isNullEmptyOrWhitespace(publicKey.get())) {
            return new ApiResponse(false, "pub is a required parameter");
        }
        if (!signedKey.isPresent() || this.helpers.isNullEmptyOrWhitespace(signedKey.get())) {
            return new ApiResponse(false, "sig is a required parameter");
        }
        String toValidate1 = null;
        try {
            toValidate1 = String.format("pub=%s&reponame=%s&repourl=%s&repotype=%s&repousername=%s&repopassword=%s&reposource=%s&repobranch=%s", URLEncoder.encode(publicKey.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(reponames.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(repourls.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(repotype.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(repousername.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(repopassword.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(reposource.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(repobranch.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()));
        } catch (UnsupportedEncodingException ex) {
            this.logger.severe(String.format("fe2464ea::error in class %s exception %s", ex.getClass(), ex.getMessage()));
            return new ApiResponse(false, "invalid signed url");
        }
        String toValidate2 = null;
        try {
            toValidate2 = String.format("pub=%s&reponame=%s&repourl=%s&repotype=%s&repousername=%s&repopassword=%s&reposource=%s&repobranch=%s&source=%s&sourceuser=%s&sourceproject=%s", URLEncoder.encode(publicKey.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(reponames.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(repourls.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(repotype.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(repousername.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(repopassword.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(reposource.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(repobranch.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(source.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(sourceuser.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()), URLEncoder.encode(sourceproject.orElse(Values.EMPTYSTRING), java.nio.charset.StandardCharsets.UTF_8.toString()));
        } catch (UnsupportedEncodingException ex) {
            this.logger.severe(String.format("f9075339::error in class %s exception %s", ex.getClass(), ex.getMessage()));
            return new ApiResponse(false, "invalid signed url");
        }
        ApiService.HmacType hmacType = hmacTypeString.orElse(Values.EMPTYSTRING).toLowerCase().equals("sha512") ? ApiService.HmacType.SHA512 : ApiService.HmacType.SHA1;
        boolean validRequest = this.validateRequest(publicKey.orElse(Values.EMPTYSTRING), signedKey.orElse(Values.EMPTYSTRING), toValidate1, hmacType);
        if (!validRequest) {
            validRequest = this.validateRequest(publicKey.orElse(Values.EMPTYSTRING), signedKey.orElse(Values.EMPTYSTRING), toValidate2, hmacType);
        }
        if (!validRequest) {
            this.logger.apiLog("b754bbe9::invalid signed repoadd call using publickey=" + publicKey);
            return new ApiResponse(false, "invalid signed url");
        }
    }
    if (repobranch.orElse(Values.EMPTYSTRING).trim().equals(Values.EMPTYSTRING)) {
        repobranch = Optional.of("master");
    }
    String repoType = repotype.orElse(Values.EMPTYSTRING).trim().toLowerCase();
    if (!Values.GIT.equals(repoType) && !Values.SVN.equals(repoType) && !Values.FILE.equals(repoType)) {
        repotype = Optional.of(Values.GIT);
    }
    Optional<RepoResult> repoResult = this.repo.getRepoByName(reponames.orElse(Values.EMPTYSTRING));
    if (repoResult.isPresent()) {
        return new ApiResponse(false, "repository name already exists");
    }
    RepoResult newRepoResult = new RepoResult().setRowId(-1).setName(reponames.orElse(Values.EMPTYSTRING)).setScm(repotype.orElse(Values.EMPTYSTRING)).setUrl(repourls.orElse(Values.EMPTYSTRING)).setUsername(repousername.orElse(Values.EMPTYSTRING)).setPassword(repopassword.orElse(Values.EMPTYSTRING)).setSource(reposource.orElse(Values.EMPTYSTRING)).setBranch(repobranch.orElse(Values.EMPTYSTRING)).setData("{}");
    // Set optional fields
    newRepoResult.getData().source = source.orElse(Values.EMPTYSTRING);
    newRepoResult.getData().project = sourceproject.orElse(Values.EMPTYSTRING);
    newRepoResult.getData().user = sourceuser.orElse(Values.EMPTYSTRING);
    ValidatorResult validate = this.validatorService.validate(newRepoResult, false);
    if (!validate.isValid) {
        return new ApiResponse(false, validate.reason);
    }
    this.repo.saveRepo(newRepoResult);
    this.logger.apiLog("ff031c29::valid signed repoadd call using publickey=" + publicKey);
    return new ApiResponse(true, "added repository successfully");
}
Also used : ValidatorResult(com.searchcode.app.model.ValidatorResult) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse) RepoResult(com.searchcode.app.model.RepoResult)

Example 2 with ValidatorResult

use of com.searchcode.app.model.ValidatorResult in project searchcode-server by boyter.

the class AdminRouteService method postRepo.

// TODO split out so we don't need the ignoreDuplicates param
public ValidatorResult postRepo(Request request, Response response, boolean ignoreDuplicates) {
    var reponames = request.queryParamsValues("reponame");
    var reposcms = request.queryParamsValues("reposcm");
    var repourls = request.queryParamsValues("repourl");
    var repousername = request.queryParamsValues("repousername");
    var repopassword = request.queryParamsValues("repopassword");
    var reposource = request.queryParamsValues("reposource");
    var repobranch = request.queryParamsValues("repobranch");
    // Additional
    var source = request.queryParamsValues("source");
    var sourceuser = request.queryParamsValues("sourceuser");
    var sourceproject = request.queryParamsValues("sourceproject");
    var validate = new ValidatorResult(true, Values.EMPTYSTRING);
    for (var i = 0; i < reponames.length; i++) {
        var branch = repobranch[i].trim();
        if (branch.equals(Values.EMPTYSTRING)) {
            branch = "master";
        }
        var repoResult = new RepoResult().setRowId(-1).setName(reponames[i]).setScm(reposcms[i]).setUrl(repourls[i]).setUsername(repousername[i]).setPassword(repopassword[i]).setSource(reposource[i]).setBranch(branch).setData("{}");
        validate = this.validatorService.validate(repoResult, ignoreDuplicates);
        repoResult.getData().source = source[i];
        repoResult.getData().user = sourceuser[i];
        repoResult.getData().project = sourceproject[i];
        if (!validate.isValid) {
            validate.setRepoResult(repoResult);
            return validate;
        }
        this.repo.saveRepo(repoResult);
        var repoByUrl = this.repo.getRepoByUrl(repourls[i]);
        repoByUrl.ifPresent(this.jobService::forceEnqueue);
    }
    return validate;
}
Also used : ValidatorResult(com.searchcode.app.model.ValidatorResult) RepoResult(com.searchcode.app.model.RepoResult)

Example 3 with ValidatorResult

use of com.searchcode.app.model.ValidatorResult in project searchcode-server by boyter.

the class ApiRouteServiceTest method testRepoAddAuthValidSigned.

public void testRepoAddAuthValidSigned() {
    Request mockRequest = mock(Request.class);
    SQLiteRepo mockSQLiteRepo = mock(SQLiteRepo.class);
    ApiService mockApiService = mock(ApiService.class);
    ValidatorService mockValidatorService = mock(ValidatorService.class);
    when(mockApiService.validateRequest("test", "test", "pub=test&reponame=test&repourl=test&repotype=test&repousername=test&repopassword=test&reposource=test&repobranch=test", ApiService.HmacType.SHA1)).thenReturn(true);
    when(mockValidatorService.validate(any(), anyBoolean())).thenReturn(new ValidatorResult(true, ""));
    when(mockSQLiteRepo.getRepoByName(anyString())).thenReturn(Optional.empty());
    ApiRouteService apiRouteService = new ApiRouteService(mockApiService, null, mockSQLiteRepo, null, mockValidatorService, null, new Helpers(), new LoggerWrapper());
    apiRouteService.apiEnabled = true;
    apiRouteService.apiAuth = true;
    when(mockRequest.queryParams("reponame")).thenReturn("test");
    when(mockRequest.queryParams("repourl")).thenReturn("test");
    when(mockRequest.queryParams("repotype")).thenReturn("test");
    when(mockRequest.queryParams("repousername")).thenReturn("test");
    when(mockRequest.queryParams("repopassword")).thenReturn("test");
    when(mockRequest.queryParams("reposource")).thenReturn("test");
    when(mockRequest.queryParams("repobranch")).thenReturn("test");
    when(mockRequest.queryParams("pub")).thenReturn("test");
    when(mockRequest.queryParams("sig")).thenReturn("test");
    ApiResponse apiResponse = apiRouteService.repoAdd(mockRequest, null);
    assertThat(apiResponse.getMessage()).isEqualTo("added repository successfully");
    assertThat(apiResponse.isSucessful()).isTrue();
    verify(mockSQLiteRepo, times(1)).saveRepo(Matchers.anyObject());
}
Also used : ApiRouteService(com.searchcode.app.service.route.ApiRouteService) ValidatorResult(com.searchcode.app.model.ValidatorResult) Helpers(com.searchcode.app.util.Helpers) LoggerWrapper(com.searchcode.app.util.LoggerWrapper) Request(spark.Request) SQLiteRepo(com.searchcode.app.dao.SQLiteRepo) ApiResponse(com.searchcode.app.dto.api.ApiResponse) RepoResultApiResponse(com.searchcode.app.dto.api.RepoResultApiResponse)

Example 4 with ValidatorResult

use of com.searchcode.app.model.ValidatorResult in project searchcode-server by boyter.

the class ValidatorServiceTest method testRepoResultNull.

public void testRepoResultNull() {
    ValidatorService validatorService = new ValidatorService();
    ValidatorResult validate = validatorService.validate(null, false);
    assertThat(validate.isValid).isFalse();
}
Also used : ValidatorResult(com.searchcode.app.model.ValidatorResult)

Example 5 with ValidatorResult

use of com.searchcode.app.model.ValidatorResult in project searchcode-server by boyter.

the class ValidatorServiceTest method testRepoResultValidReponame.

public void testRepoResultValidReponame() {
    ValidatorService validatorService = new ValidatorService();
    ValidatorResult validate = validatorService.validate(new RepoResult().setRowId(0).setName("some_thing").setScm("something").setUrl("url").setUsername("").setPassword("").setSource("source").setBranch("branch").setData("{}"), false);
    assertThat(validate.isValid).isTrue();
}
Also used : ValidatorResult(com.searchcode.app.model.ValidatorResult) RepoResult(com.searchcode.app.model.RepoResult)

Aggregations

ValidatorResult (com.searchcode.app.model.ValidatorResult)16 RepoResult (com.searchcode.app.model.RepoResult)12 SQLiteRepo (com.searchcode.app.dao.SQLiteRepo)5 Helpers (com.searchcode.app.util.Helpers)4 ApiResponse (com.searchcode.app.dto.api.ApiResponse)3 RepoResultApiResponse (com.searchcode.app.dto.api.RepoResultApiResponse)3 Request (spark.Request)3 ApiRouteService (com.searchcode.app.service.route.ApiRouteService)2 LoggerWrapper (com.searchcode.app.util.LoggerWrapper)2 AdminRouteService (com.searchcode.app.service.route.AdminRouteService)1 JsonTransformer (com.searchcode.app.util.JsonTransformer)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 HashMap (java.util.HashMap)1 ModelAndView (spark.ModelAndView)1 FreeMarkerEngine (spark.template.freemarker.FreeMarkerEngine)1