Search in sources :

Example 41 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project azure-sdk-for-java by Azure.

the class ManageFunctionAppSourceControl method runSample.

/**
     * Main function which runs the actual sample.
     * @param azure instance of the azure client
     * @return true if sample runs successfully
     */
public static boolean runSample(Azure azure) {
    // New resources
    final String suffix = ".azurewebsites.net";
    final String app1Name = SdkContext.randomResourceName("webapp1-", 20);
    final String app2Name = SdkContext.randomResourceName("webapp2-", 20);
    final String app3Name = SdkContext.randomResourceName("webapp3-", 20);
    final String app4Name = SdkContext.randomResourceName("webapp4-", 20);
    final String app1Url = app1Name + suffix;
    final String app2Url = app2Name + suffix;
    final String app3Url = app3Name + suffix;
    final String app4Url = app4Name + suffix;
    final String rgName = SdkContext.randomResourceName("rg1NEMV_", 24);
    try {
        //============================================================
        // Create a function app with a new app service plan
        System.out.println("Creating function app " + app1Name + " in resource group " + rgName + "...");
        FunctionApp app1 = azure.appServices().functionApps().define(app1Name).withRegion(Region.US_WEST).withNewResourceGroup(rgName).create();
        System.out.println("Created function app " + app1.name());
        Utils.print(app1);
        //============================================================
        // Deploy to app 1 through FTP
        System.out.println("Deploying a function app to " + app1Name + " through FTP...");
        Utils.uploadFileToFtp(app1.getPublishingProfile(), "host.json", ManageFunctionAppSourceControl.class.getResourceAsStream("/square-function-app/host.json"));
        Utils.uploadFileToFtp(app1.getPublishingProfile(), "square/function.json", ManageFunctionAppSourceControl.class.getResourceAsStream("/square-function-app/square/function.json"));
        Utils.uploadFileToFtp(app1.getPublishingProfile(), "square/index.js", ManageFunctionAppSourceControl.class.getResourceAsStream("/square-function-app/square/index.js"));
        System.out.println("Deployment square app to function app " + app1.name() + " completed");
        Utils.print(app1);
        // warm up
        System.out.println("Warming up " + app1Url + "/api/square...");
        post("http://" + app1Url + "/api/square", "625");
        Thread.sleep(5000);
        System.out.println("CURLing " + app1Url + "/api/square...");
        System.out.println("Square of 625 is " + post("http://" + app1Url + "/api/square", "625"));
        //============================================================
        // Create a second function app with local git source control
        System.out.println("Creating another function app " + app2Name + " in resource group " + rgName + "...");
        AppServicePlan plan = azure.appServices().appServicePlans().getById(app1.appServicePlanId());
        FunctionApp app2 = azure.appServices().functionApps().define(app2Name).withExistingAppServicePlan(plan).withExistingResourceGroup(rgName).withExistingStorageAccount(app1.storageAccount()).withLocalGitSourceControl().create();
        System.out.println("Created function app " + app2.name());
        Utils.print(app2);
        //============================================================
        // Deploy to app 2 through local Git
        System.out.println("Deploying a local Tomcat source to " + app2Name + " through Git...");
        PublishingProfile profile = app2.getPublishingProfile();
        Git git = Git.init().setDirectory(new File(ManageFunctionAppSourceControl.class.getResource("/square-function-app/").getPath())).call();
        git.add().addFilepattern(".").call();
        git.commit().setMessage("Initial commit").call();
        PushCommand command = git.push();
        command.setRemote(profile.gitUrl());
        command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(profile.gitUsername(), profile.gitPassword()));
        command.setRefSpecs(new RefSpec("master:master"));
        command.setForce(true);
        command.call();
        System.out.println("Deployment to function app " + app2.name() + " completed");
        Utils.print(app2);
        // warm up
        System.out.println("Warming up " + app2Url + "/api/square...");
        post("http://" + app2Url + "/api/square", "725");
        Thread.sleep(5000);
        System.out.println("CURLing " + app2Url + "/api/square...");
        System.out.println("Square of 725 is " + post("http://" + app2Url + "/api/square", "725"));
        //============================================================
        // Create a 3rd function app with a public GitHub repo in Azure-Samples
        System.out.println("Creating another function app " + app3Name + "...");
        FunctionApp app3 = azure.appServices().functionApps().define(app3Name).withExistingAppServicePlan(plan).withNewResourceGroup(rgName).withExistingStorageAccount(app2.storageAccount()).defineSourceControl().withPublicGitRepository("https://github.com/jianghaolu/square-function-app-sample").withBranch("master").attach().create();
        System.out.println("Created function app " + app3.name());
        Utils.print(app3);
        // warm up
        System.out.println("Warming up " + app3Url + "/api/square...");
        post("http://" + app3Url + "/api/square", "825");
        Thread.sleep(5000);
        System.out.println("CURLing " + app3Url + "/api/square...");
        System.out.println("Square of 825 is " + post("http://" + app3Url + "/api/square", "825"));
        //============================================================
        // Create a 4th function app with a personal GitHub repo and turn on continuous integration
        System.out.println("Creating another function app " + app4Name + "...");
        FunctionApp app4 = azure.appServices().functionApps().define(app4Name).withExistingAppServicePlan(plan).withExistingResourceGroup(rgName).withExistingStorageAccount(app3.storageAccount()).create();
        System.out.println("Created function app " + app4.name());
        Utils.print(app4);
        // warm up
        System.out.println("Warming up " + app4Url + "...");
        curl("http://" + app4Url);
        Thread.sleep(5000);
        System.out.println("CURLing " + app4Url + "...");
        System.out.println(curl("http://" + app4Url));
        return true;
    } catch (Exception e) {
        System.err.println(e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            System.out.println("Deleting Resource Group: " + rgName);
            azure.resourceGroups().beginDeleteByName(rgName);
            System.out.println("Deleted Resource Group: " + rgName);
        } catch (NullPointerException npe) {
            System.out.println("Did not create any resources in Azure. No clean up is necessary");
        } catch (Exception g) {
            g.printStackTrace();
        }
    }
    return false;
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) Git(org.eclipse.jgit.api.Git) RefSpec(org.eclipse.jgit.transport.RefSpec) PublishingProfile(com.microsoft.azure.management.appservice.PublishingProfile) FunctionApp(com.microsoft.azure.management.appservice.FunctionApp) File(java.io.File) PushCommand(org.eclipse.jgit.api.PushCommand) IOException(java.io.IOException) AppServicePlan(com.microsoft.azure.management.appservice.AppServicePlan)

Example 42 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project spring-cloud-config by spring-cloud.

the class JGitEnvironmentRepositoryTests method gitCredentialsProviderFactoryCreatesUsernamePasswordProvider.

@Test
public void gitCredentialsProviderFactoryCreatesUsernamePasswordProvider() throws Exception {
    GitCredentialsProviderFactory credentialsFactory = new GitCredentialsProviderFactory();
    Git mockGit = mock(Git.class);
    MockCloneCommand mockCloneCommand = new MockCloneCommand(mockGit);
    final String username = "someuser";
    final String password = "mypassword";
    JGitEnvironmentRepository envRepository = new JGitEnvironmentRepository(this.environment, new JGitEnvironmentProperties());
    envRepository.setGitFactory(new MockGitFactory(mockGit, mockCloneCommand));
    envRepository.setUri("git+ssh://git@somegitserver/somegitrepo");
    envRepository.setBasedir(new File("./mybasedir"));
    envRepository.setGitCredentialsProvider(credentialsFactory.createFor(envRepository.getUri(), username, password, null));
    envRepository.setCloneOnStart(true);
    envRepository.afterPropertiesSet();
    assertTrue(mockCloneCommand.getCredentialsProvider() instanceof UsernamePasswordCredentialsProvider);
    CredentialsProvider provider = mockCloneCommand.getCredentialsProvider();
    CredentialItem.Username usernameCredential = new CredentialItem.Username();
    CredentialItem.Password passwordCredential = new CredentialItem.Password();
    assertTrue(provider.supports(usernameCredential));
    assertTrue(provider.supports(passwordCredential));
    provider.get(new URIish(), usernameCredential);
    assertEquals(usernameCredential.getValue(), username);
    provider.get(new URIish(), passwordCredential);
    assertEquals(String.valueOf(passwordCredential.getValue()), password);
}
Also used : URIish(org.eclipse.jgit.transport.URIish) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialItem(org.eclipse.jgit.transport.CredentialItem) Matchers.anyString(org.mockito.Matchers.anyString) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) PassphraseCredentialsProvider(org.springframework.cloud.config.server.support.PassphraseCredentialsProvider) Git(org.eclipse.jgit.api.Git) GitCredentialsProviderFactory(org.springframework.cloud.config.server.support.GitCredentialsProviderFactory) File(java.io.File) Test(org.junit.Test)

Example 43 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project spring-cloud-config by spring-cloud.

the class GitCredentialsProviderFactory method createFor.

/**
 * Search for a credential provider that will handle the specified URI. If
 * not found, and the username has text, then create a default using the
 * provided username and password. Otherwise null.
 * @param uri the URI of the repository (cannot be null)
 * @param username the username provided for the repository (may be null)
 * @param password the password provided for the repository (may be null)
 * @param passphrase the passphrase to unlock the ssh private key (may be null)
 * @return the first matched credentials provider or the default or null.
 */
public CredentialsProvider createFor(String uri, String username, String password, String passphrase) {
    CredentialsProvider provider = null;
    if (awsAvailable() && AwsCodeCommitCredentialProvider.canHandle(uri)) {
        logger.debug("Constructing AwsCodeCommitCredentialProvider for URI " + uri);
        AwsCodeCommitCredentialProvider aws = new AwsCodeCommitCredentialProvider();
        aws.setUsername(username);
        aws.setPassword(password);
        provider = aws;
    } else if (hasText(username)) {
        logger.debug("Constructing UsernamePasswordCredentialsProvider for URI " + uri);
        provider = new UsernamePasswordCredentialsProvider(username, password.toCharArray());
    } else if (hasText(passphrase)) {
        logger.debug("Constructing PassphraseCredentialsProvider for URI " + uri);
        provider = new PassphraseCredentialsProvider(passphrase);
    } else {
        logger.debug("No credentials provider required for URI " + uri);
    }
    return provider;
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider)

Example 44 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project spring-cloud-config by spring-cloud.

the class GitCredentialsProviderFactoryTests method testCreateForServerWithUsername.

@Test
public void testCreateForServerWithUsername() {
    CredentialsProvider provider = factory.createFor(GIT_REPO, USER, PASSWORD, null);
    assertNotNull(provider);
    assertTrue(provider instanceof UsernamePasswordCredentialsProvider);
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) PassphraseCredentialsProvider(org.springframework.cloud.config.server.support.PassphraseCredentialsProvider) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) Test(org.junit.Test)

Example 45 with UsernamePasswordCredentialsProvider

use of org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider in project spring-cloud-config by spring-cloud.

the class GitCredentialsProviderFactoryTests method testCreateForAwsDisabled.

@Test
public void testCreateForAwsDisabled() {
    factory.setAwsCodeCommitEnabled(false);
    CredentialsProvider provider = factory.createFor(AWS_REPO, null, null, null);
    assertNull(provider);
    provider = factory.createFor(AWS_REPO, USER, PASSWORD, null);
    assertNotNull(provider);
    assertTrue(provider instanceof UsernamePasswordCredentialsProvider);
}
Also used : UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) PassphraseCredentialsProvider(org.springframework.cloud.config.server.support.PassphraseCredentialsProvider) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) CredentialsProvider(org.eclipse.jgit.transport.CredentialsProvider) Test(org.junit.Test)

Aggregations

UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)76 Git (org.eclipse.jgit.api.Git)47 File (java.io.File)34 CredentialsProvider (org.eclipse.jgit.transport.CredentialsProvider)23 IOException (java.io.IOException)19 Test (org.junit.Test)18 CloneCommand (org.eclipse.jgit.api.CloneCommand)17 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)15 RepositoryModel (com.gitblit.models.RepositoryModel)12 PushResult (org.eclipse.jgit.transport.PushResult)12 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)9 FileOutputStream (java.io.FileOutputStream)8 PushCommand (org.eclipse.jgit.api.PushCommand)8 RevCommit (org.eclipse.jgit.revwalk.RevCommit)8 BufferedWriter (java.io.BufferedWriter)7 OutputStreamWriter (java.io.OutputStreamWriter)7 Date (java.util.Date)7 Ref (org.eclipse.jgit.lib.Ref)7 Repository (org.eclipse.jgit.lib.Repository)7 URIish (org.eclipse.jgit.transport.URIish)7