Search code examples
mavenbitbucket-pipelinesmulesoftanypoint-rtf

Mulesoft Maven Deploy To RTF 401 Unauthorized


I am attempting to deploy to Mulesoft from a BitBucket Pipeline using Maven. The deployment target is an RTF instance that is configured for inbound traffic. I am using the following bitbucket-pipelines.yml:

image: maven:3.6.1

pipelines:
  branches:
    develop:
      - step:
          caches:
            - maven
          name: Deploy to test
          deployment: test
          script:
            - mvn clean package deploy -e -DskipMunitTests -DmuleDeploy -Dapp.name=$env.APPLICATION_NAME -Dgroup.id=$env.MULESOFT_GROUP_ID -Dapp.id=$env.MULESOFT_CONNECTED_APP_ID_DEV -Dapp.secret=$env.MULESOFT_CONNECTED_APP_DEV_SECRET -Drtf.name=$env.RUNTIME_FABRIC_NAME -Drtf.environment=$env.ANYPOINT_ENVIRONMENT -Dbusiness.group=$env.MULESOFT_BUSINESS_GROUP

This is my pom.xml:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>GROUP GUID</groupId>
    <artifactId>APP NAME</artifactId>
    <version>1.0.0</version>
    <packaging>mule-application</packaging>

    <name>APP NAME</name>

    <description>APP DESCRIPTION</description>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <app.runtime>4.3.0</app.runtime>
        <mule.maven.plugin.version>3.4.1</mule.maven.plugin.version>
        <app.name>${env.APP_NAME}</app.name>
        <group.id>${env.GROUP_ID}</group.id>
        <app.id>${env.APP_ID}</app.id>
        <app.secret>${env.APP_SECRET}</app.secret>
        <rtf.name>${env.RTF_NAME}</rtf.name>
        <rtf.environment>${env.RTF_ENVIRONMENT}</rtf.environment>
        <business.group>${env.BUSINESS_GROUP}</business.group>
    </properties>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-clean-plugin</artifactId>
                <version>3.1.0</version>
            </plugin>
            <plugin>
                <groupId>org.mule.tools.maven</groupId>
                <artifactId>mule-maven-plugin</artifactId>
                <version>3.4.1</version>
                <extensions>true</extensions>
                <configuration>
                    <runtimeFabricDeployment>
                        <muleVersion>${app.runtime}</muleVersion>
                        <connectedAppClientId>${app.id}</connectedAppClientId>
                        <connectedAppClientSecret>${app.secret}</connectedAppClientSecret>
                        <connectedAppGrantType>client_credentials</connectedAppGrantType>
                        <applicationName>${app.name}</applicationName>
                        <target>${rtf.name}</target>
                        <environment>${rtf.environment}</environment>
                        <businessGroup>${business.group}</businessGroup>
                        <provider>MC</provider>
                    </runtimeFabricDeployment>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>org.mule.connectors</groupId>
            <artifactId>mule-http-connector</artifactId>
            <version>1.5.19</version>
            <classifier>mule-plugin</classifier>
        </dependency>
        <dependency>
            <groupId>org.mule.connectors</groupId>
            <artifactId>mule-sockets-connector</artifactId>
            <version>1.2.0</version>
            <classifier>mule-plugin</classifier>
        </dependency>
        <dependency>
            <groupId>org.mule.modules</groupId>
            <artifactId>mule-apikit-module</artifactId>
            <version>1.3.13</version>
            <classifier>mule-plugin</classifier>
        </dependency>
    </dependencies>

    <repositories>
          <repository>
            <id>anypoint-exchange-v2</id>
            <name>Anypoint Exchange</name>
            <url>https://maven.anypoint.mulesoft.com/api/v2/maven</url>
            <layout>default</layout>
        </repository>
        <repository>
            <id>mulesoft-releases</id>
            <name>MuleSoft Releases Repository</name>
            <url>https://repository.mulesoft.org/releases/</url>
            <layout>default</layout>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>mulesoft-releases</id>
            <name>mulesoft release repository</name>
            <layout>default</layout>
            <url>https://repository.mulesoft.org/releases/</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
        <pluginRepository>
            <id>mule-public</id>
            <url>https://repository.mulesoft.org/nexus/content/repositories/releases</url>
        </pluginRepository>
    </pluginRepositories>

</project>

I am now receiving the following error from the pipeline:

[INFO] --- mule-maven-plugin:3.4.1:deploy (default-deploy) @ APP NAME ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  02:00 min
[INFO] Finished at: 2020-09-11T15:24:36Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.mule.tools.maven:mule-maven-plugin:3.4.1:deploy (default-deploy) on project APP NAME: Execution default-deploy of goal org.mule.tools.maven:mule-maven-plugin:3.4.1:deploy failed: 401 Unauthorized: Unauthorized -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.mule.tools.maven:mule-maven-plugin:3.4.1:deploy (default-deploy) on project APP NAME: Execution default-deploy of goal org.mule.tools.maven:mule-maven-plugin:3.4.1:deploy failed: 401 Unauthorized: Unauthorized
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-deploy of goal org.mule.tools.maven:mule-maven-plugin:3.4.1:deploy failed: 401 Unauthorized: Unauthorized
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:148)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:210)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:956)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:288)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:192)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.mule.tools.client.core.exception.ClientException: 401 Unauthorized: Unauthorized
    at org.mule.tools.client.core.AbstractClient.checkResponseStatus (AbstractClient.java:175)
    at org.mule.tools.client.authentication.AuthenticationServiceClient.loginWithConnectedApp (AuthenticationServiceClient.java:152)
    at org.mule.tools.client.authentication.AuthenticationServiceClient.getBearerTokenForConnectedApp (AuthenticationServiceClient.java:91)
    at org.mule.tools.client.AbstractMuleClient.getBearerToken (AbstractMuleClient.java:347)
    at org.mule.tools.client.AbstractMuleClient.init (AbstractMuleClient.java:102)
    at org.mule.tools.client.core.AbstractClient.initialize (AbstractClient.java:123)
    at org.mule.tools.client.core.AbstractClient.get (AbstractClient.java:93)
    at org.mule.tools.client.fabric.RuntimeFabricClient.getDeployments (RuntimeFabricClient.java:55)
    at org.mule.tools.verification.fabric.RuntimeFabricDeploymentVerification$RuntimeFabricDeploymentVerificationStrategy.<init> (RuntimeFabricDeploymentVerification.java:51)
    at org.mule.tools.verification.fabric.RuntimeFabricDeploymentVerification$RuntimeFabricDeploymentVerificationStrategy.<init> (RuntimeFabricDeploymentVerification.java:45)
    at org.mule.tools.verification.fabric.RuntimeFabricDeploymentVerification.<init> (RuntimeFabricDeploymentVerification.java:37)
    at org.mule.tools.deployment.fabric.RuntimeFabricArtifactDeployer.<init> (RuntimeFabricArtifactDeployer.java:50)
    at org.mule.tools.deployment.fabric.RuntimeFabricArtifactDeployer.<init> (RuntimeFabricArtifactDeployer.java:41)
    at org.mule.tools.deployment.fabric.RuntimeFabricApplicationDeployer.<init> (RuntimeFabricApplicationDeployer.java:25)
    at org.mule.tools.deployment.AbstractDeployerFactory$RuntimeFabricDeployerFactory.createArtifactDeployer (AbstractDeployerFactory.java:170)
    at org.mule.tools.deployment.DefaultDeployer.<init> (DefaultDeployer.java:36)

I have made sure to provide the permissions to the connected app from the following document: https://help.mulesoft.com/s/article/The-Minimum-Permission-to-Deploy-an-Application-with-Anypoint-Console-or-Maven-in-RTF

My organization is sub-organization, does the connected app need permission to the master organization? Am I missing a permission item that I didn't see? I am also not very familiar with BitBucket Pipelines, do I need to add anything to my YAML? Thanks for your help in advance.


Solution

  • The issue has been solved. There were a couple of problems. First, my Bitbucket pipeline was using the following format for injecting repository variables: $env.VAR_NAME. It is not needed to use the env. prefix. Those were all changed to be $VAR_NAME.

    Next, my solution belongs to a Sub-Org and the full name has spaces in it. I changed this to use businessGroupId, and I used the GUID for the sub-org. Together, these allowed the maven deploy to work correctly.

    NOTE: I also found out that you cannot use the BitBucket pipeline with the debug switch on. It times out while downloading all the dependencies.