Skip to content

Commit bc3dfc6

Browse files
authored
Added support for ngrok authToken (#61)
* Added support for ngrok authToken * added auth token to test app * test fixes * CI tests * Fixes automation tests * CI fix * CI fix vNEXT * CI fix vNEXT * CI fix vNEXT * CI fix vNEXT * CI fix vNEXT * CI fix vNEXT * CI fix vNEXT * dependencies versions bump * added docs about authToken
1 parent 1de4d39 commit bc3dfc6

16 files changed

+127
-243
lines changed

.github/scripts/set-current-starter-version-macos.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

.github/scripts/set-current-starter-version.sh

Lines changed: 0 additions & 9 deletions
This file was deleted.

.github/test-app/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<description>Dummy app for tests</description>
1616
<properties>
1717
<java.version>11</java.version>
18-
<ngrok-spring-boot-starter.version>ci-version-placeholder</ngrok-spring-boot-starter.version>
18+
<ngrok-spring-boot-starter.version>SNAPSHOT</ngrok-spring-boot-starter.version>
1919
</properties>
2020
<dependencies>
2121
<dependency>

.github/test-app/src/test/java/io/github/kilmajster/AppTests.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,12 @@
2020

2121
import static org.assertj.core.api.Assertions.assertThat;
2222

23-
@SpringBootTest(
24-
webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT,
25-
properties = {
26-
"ngrok.enabled=true"
27-
}
28-
)
23+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
2924
@ExtendWith(OutputCaptureExtension.class)
3025
class AppTests {
3126

3227
private final static Logger log = LoggerFactory.getLogger(AppTests.class);
33-
private static final int WAIT_FOR_STARTUP_SECONDS = 30;
28+
private static final int WAIT_FOR_STARTUP_SECONDS = 90;
3429
private static final String HTTPS_NGROK_TUNNEL_REGEX = "(https:\\/\\/)?(([^.]+)\\.)?ngrok\\.io";
3530

3631
@Test
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ngrok.enabled=true
2+

.github/workflows/automation-test-macos.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ jobs:
1919
- name: Silent install main project
2020
run: mvn --batch-mode --no-transfer-progress install -Dmaven.test.skip=true
2121

22-
- name: Install xmllint
23-
run: brew install xmlstarlet
24-
25-
- name: Set current starter version in test-app
26-
working-directory: ./.github/scripts/
27-
run: sh set-current-starter-version-macos.sh
22+
- name: Add ngrok auth token application.properties
23+
run: |
24+
printf ${{secrets.NGROK_AUTH_TOKEN_PROPERTY}} >> .github/test-app/src/test/resources/application.properties
25+
shell: bash
2826

2927
- name: Run automation tests with latest ngrok-spring-boot-starter version on test-app
3028
working-directory: ./.github/test-app/

.github/workflows/automation-test-ubuntu.yml

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ jobs:
1919
- name: Silent install main project
2020
run: mvn --batch-mode --no-transfer-progress install -Dmaven.test.skip=true
2121

22-
- name: Install xmllint
23-
run: sudo apt-get install libxml2-utils
24-
25-
- name: Set current starter version in test-app
26-
working-directory: ./.github/scripts/
27-
run: sh set-current-starter-version.sh
22+
- name: Add ngrok auth token application.properties
23+
run: |
24+
printf ${{secrets.NGROK_AUTH_TOKEN_PROPERTY}} >> .github/test-app/src/test/resources/application.properties
25+
shell: bash
2826

2927
- name: Run automation tests with latest ngrok-spring-boot-starter version on test-app
3028
working-directory: ./.github/test-app/

.github/workflows/automation-test-windows.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ jobs:
1919
- name: Silent install main project
2020
run: mvn --batch-mode --no-transfer-progress install -DskipTests
2121

22-
- name: Set current starter version in test-app
23-
working-directory: ./.github/scripts/
24-
run: sh set-current-starter-version.sh
22+
- name: Add ngrok auth token application.properties
23+
run: |
24+
printf ${{secrets.NGROK_AUTH_TOKEN_PROPERTY}} >> .github/test-app/src/test/resources/application.properties
25+
shell: bash
2526

2627
- name: Run automation tests with latest ngrok-spring-boot-starter version on test-app
2728
working-directory: ./.github/test-app/

README.md

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,19 @@ compile('io.github.kilmajster:ngrok-spring-boot-starter:0.5.0')
5959
6060
## Configuration
6161
### 🚀 Minimal configuration
62-
#### `ngrok.enabled` - one required configuration property
62+
#### `ngrok.enabled`
6363
For simple http tunneling to springs default server port, **only one configuration property is required**.
6464
There are many ways to provide spring config, for `application.property` based one, it will be:
6565
```properties
6666
ngrok.enabled=true
6767
```
68-
or yaml:
69-
```yaml
70-
ngrok:
71-
enabled: true
68+
69+
#### `ngrok.authToken`
70+
Ngrok requires `authToken` to be defined, to obtain one visit https://dashboard.ngrok.com/get-started/your-authtoken and then add it like below:
71+
```properties
72+
ngrok.authToken=<YOUR PERSONAL AUTH TOKEN>
7273
```
73-
or you can pass java execution attribute, like: `java -jar your-app.jar --ngrok.enabled=true`,
74-
or with spring-boot plugin `mvn spring-boot:run -Dspring-boot.run.arguments="--ngrok.enabled=true"`, or any other way 😊
74+
If you got already configured auth token in your ngrok config file there is no need to define this property.
7575

7676
**All done, configuration is ready!**
7777

@@ -121,21 +121,6 @@ ngrok.command=http -region=us -hostname=dev.example.com 8000
121121
# should be = /home/user/.ngrok2/ngrok http -region=us -hostname=dev.example.com 8000
122122
```
123123
124-
## Ngrok API
125-
To access ngrok tunneling details you can use `NgrokApiClient` bean. Example below:
126-
```java
127-
@Autowired
128-
private NgrokApiClient ngrok;
129-
130-
public void someMethod() {
131-
# returns https tunnel URL or null in case ngrok is not running
132-
String httpsTunnelUrl = ngrok.getHttpsTunnelUrl();
133-
134-
# returns http tunnel URL or null in case ngrok is not running
135-
String httpTunnelUrl = ngrok.getHttpTunnelUrl();
136-
}
137-
```
138-
139124
##### Optional properties & descriptions
140125
```properties
141126
# if you've got already running Ngrok instance somewhere else, you can specify its host & port, whoch defaults are:

README_v2.md

Lines changed: 0 additions & 175 deletions
This file was deleted.

pom.xml

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
<parent>
6060
<groupId>org.springframework.boot</groupId>
6161
<artifactId>spring-boot-starter-parent</artifactId>
62-
<version>2.4.5</version>
62+
<version>2.6.6</version>
6363
<relativePath/>
6464
</parent>
6565

@@ -75,9 +75,10 @@
7575

7676
<commons-io.version>2.11.0</commons-io.version>
7777
<commons-lang3.version>3.12.0</commons-lang3.version>
78+
<jackson-dataformat-yaml.version>2.13.2</jackson-dataformat-yaml.version>
7879
<vavr.version>0.10.4</vavr.version>
7980

80-
<spring-cloud-contract-wiremock.version>3.0.3</spring-cloud-contract-wiremock.version>
81+
<spring-cloud-contract-wiremock.version>3.1.1</spring-cloud-contract-wiremock.version>
8182
</properties>
8283

8384
<dependencies>
@@ -111,6 +112,12 @@
111112
<version>${vavr.version}</version>
112113
</dependency>
113114

115+
<dependency>
116+
<groupId>com.fasterxml.jackson.dataformat</groupId>
117+
<artifactId>jackson-dataformat-yaml</artifactId>
118+
<version>${jackson-dataformat-yaml.version}</version>
119+
</dependency>
120+
114121
<dependency>
115122
<groupId>commons-io</groupId>
116123
<artifactId>commons-io</artifactId>

src/main/java/ngrok/NgrokRunner.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public void run(WebServerInitializedEvent event) throws NgrokDownloadException,
5151
downloadAndExtractNgrokBinary();
5252
addPermissionsIfNeeded();
5353
}
54+
55+
configureAuthTokenOrLogWarn();
56+
5457
startNgrok(port);
5558
tunnels = ngrokApiClient.listTunnels(port);
5659
} else {
@@ -70,6 +73,26 @@ public void run(WebServerInitializedEvent event) throws NgrokDownloadException,
7073
});
7174
}
7275

76+
private void configureAuthTokenOrLogWarn() {
77+
final String ngrokDirectory = ngrokBinaryProvider.getNgrokDirectoryOrDefault();
78+
if (!ngrokConfigurationProvider.isAuthTokenConfigured(ngrokDirectory)) {
79+
if (!ngrokConfigurationProvider.isAuthTokenPresent(ngrokDirectory)) {
80+
log.warn("Ngrok auth token is missing! For your personal auth token visit https://dashboard.ngrok.com/get-started/your-authtoken " +
81+
"and then add it as ngrok.authToken=<YOUR AUTH TOKEN> to application.properties or to your ngrok configuration file.");
82+
} else {
83+
configureAuthToken();
84+
}
85+
}
86+
}
87+
88+
private void configureAuthToken() {
89+
String command = ngrokBinaryProvider.getNgrokBinaryFilePath()
90+
+ " authtoken "
91+
+ ngrokConfiguration.getAuthToken();
92+
93+
ngrokSystemCommandExecutor.execute(command);
94+
}
95+
7396
@SafeVarargs
7497
private static <T> List<T> listOf(T... args) {
7598
return new ArrayList<>(Arrays.asList(args));

0 commit comments

Comments
 (0)