From 5f5c396b50a3539fe5c2b98928356f8184d7315e Mon Sep 17 00:00:00 2001 From: lastpeony Date: Tue, 16 Apr 2024 17:12:08 +0300 Subject: [PATCH 1/5] webhook play auth --- src/main/java/io/antmedia/AppSettings.java | 28 +++++++++++++++++++ .../AcceptOnlyStreamsWithWebhook.java | 3 +- .../io/antmedia/test/AppSettingsUnitTest.java | 7 +++-- .../AcceptOnlyStreamsWithWebhookTest.java | 2 ++ 4 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/antmedia/AppSettings.java b/src/main/java/io/antmedia/AppSettings.java index 9d2e1eb6a..b26818ec1 100644 --- a/src/main/java/io/antmedia/AppSettings.java +++ b/src/main/java/io/antmedia/AppSettings.java @@ -2113,6 +2113,18 @@ public boolean isWriteStatsToDatastore() { @Value("${webhookRetryAttemptDelay:1000}") private long webhookRetryDelay = 1000; + /** + * Enable webhook authentication for webrtc. play + */ + @Value("${webhookPlayAuthEnabled:false}") + private boolean webhookPlayAuthEnabled = false; + + /** + * Enable webhook authentication for publish. + */ + @Value("${webhookPublishAuthEnabled:false}") + private boolean webhookPublishAuthEnabled = false; + public void setWriteStatsToDatastore(boolean writeStatsToDatastore) { this.writeStatsToDatastore = writeStatsToDatastore; } @@ -3665,4 +3677,20 @@ public void setWebhookRetryDelay(long webhookRetryDelay) { this.webhookRetryDelay = webhookRetryDelay; } + public boolean isWebhookPlayAuthEnabled() { + return webhookPlayAuthEnabled; + } + + public void setWebhookPlayAuthEnabled(boolean webhookPlayAuthEnabled) { + this.webhookPlayAuthEnabled = webhookPlayAuthEnabled; + } + + public boolean isWebhookPublishAuthEnabled() { + return webhookPublishAuthEnabled; + } + + public void setWebhookPublishAuthEnabled(boolean webhookPublishAuthEnabled) { + this.webhookPublishAuthEnabled = webhookPublishAuthEnabled; + } + } diff --git a/src/main/java/io/antmedia/security/AcceptOnlyStreamsWithWebhook.java b/src/main/java/io/antmedia/security/AcceptOnlyStreamsWithWebhook.java index 9b3c391a0..edd91ff2c 100644 --- a/src/main/java/io/antmedia/security/AcceptOnlyStreamsWithWebhook.java +++ b/src/main/java/io/antmedia/security/AcceptOnlyStreamsWithWebhook.java @@ -42,7 +42,8 @@ public synchronized boolean isPublishAllowed(IScope scope, String streamId, Stri appSettings = (AppSettings) scope.getContext().getBean(AppSettings.BEAN_NAME); } final String webhookAuthURL = appSettings.getWebhookAuthenticateURL(); - if (webhookAuthURL != null && !webhookAuthURL.isEmpty()) + boolean publishWebhookAuthEnabled = appSettings.isWebhookPublishAuthEnabled(); + if (publishWebhookAuthEnabled && webhookAuthURL != null && !webhookAuthURL.isEmpty()) { try (CloseableHttpClient client = getHttpClient()) { diff --git a/src/test/java/io/antmedia/test/AppSettingsUnitTest.java b/src/test/java/io/antmedia/test/AppSettingsUnitTest.java index 9ef53aece..0bf2a3033 100644 --- a/src/test/java/io/antmedia/test/AppSettingsUnitTest.java +++ b/src/test/java/io/antmedia/test/AppSettingsUnitTest.java @@ -526,7 +526,7 @@ public void testUnsetAppSettings(AppSettings appSettings) { assertNull(appSettings.getTimeTokenSecretForPublish()); assertNull(appSettings.getTimeTokenSecretForPlay()); - assertEquals(true, appSettings.isHwScalingEnabled()); + assertTrue(appSettings.isHwScalingEnabled()); assertNotNull(appSettings.getSubscriberAuthenticationKey()); assertNull(appSettings.getFirebaseAccountKeyJSON()); @@ -538,12 +538,15 @@ public void testUnsetAppSettings(AppSettings appSettings) { assertEquals(0, appSettings.getWebhookRetryCount()); assertEquals(1000, appSettings.getWebhookRetryDelay()); + assertFalse(appSettings.isWebhookPublishAuthEnabled()); + assertFalse(appSettings.isWebhookPlayAuthEnabled()); + //if we add a new field, we just need to check its default value in this test //When a new field is added or removed please update the number of fields and make this test pass //by also checking its default value. assertEquals("New field is added to settings. PAY ATTENTION: Please CHECK ITS DEFAULT VALUE and fix the number of fields.", - 177, numberOfFields); + 179, numberOfFields); } diff --git a/src/test/java/io/antmedia/test/security/AcceptOnlyStreamsWithWebhookTest.java b/src/test/java/io/antmedia/test/security/AcceptOnlyStreamsWithWebhookTest.java index cff0b09dd..35d58886d 100644 --- a/src/test/java/io/antmedia/test/security/AcceptOnlyStreamsWithWebhookTest.java +++ b/src/test/java/io/antmedia/test/security/AcceptOnlyStreamsWithWebhookTest.java @@ -44,6 +44,7 @@ public void testAcceptOnlyStreamsWithWebhook() AppSettings appSettings = new AppSettings(); appSettings.setWebhookAuthenticateURL("sampleurl"); + appSettings.setWebhookPublishAuthEnabled(true); filter.setAppSettings(appSettings); boolean publishAllowed = filter.isPublishAllowed(scope, "streamId", "mode", null, null); assertFalse(publishAllowed); @@ -56,6 +57,7 @@ public void testIsPublishAllowedWithWebhook() throws IOException { IScope scope = Mockito.mock(IScope.class); AppSettings appSettings = new AppSettings(); appSettings.setWebhookAuthenticateURL("asd"); + appSettings.setWebhookPublishAuthEnabled(true); InMemoryDataStore dataStore = new InMemoryDataStore("db"); DataStoreFactory factory = Mockito.mock(DataStoreFactory.class); From 5206774bb51a4afd749a374212d1554ed76e7178 Mon Sep 17 00:00:00 2001 From: lastpeony Date: Mon, 29 Apr 2024 21:15:47 +0300 Subject: [PATCH 2/5] refactor for bacwards comp --- src/main/java/io/antmedia/AppSettings.java | 25 ++++++------------- .../AcceptOnlyStreamsWithWebhook.java | 7 +++--- .../io/antmedia/test/AppSettingsUnitTest.java | 8 ++++-- .../AcceptOnlyStreamsWithWebhookTest.java | 2 -- 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/src/main/java/io/antmedia/AppSettings.java b/src/main/java/io/antmedia/AppSettings.java index b26818ec1..453b2b2fa 100644 --- a/src/main/java/io/antmedia/AppSettings.java +++ b/src/main/java/io/antmedia/AppSettings.java @@ -2114,16 +2114,11 @@ public boolean isWriteStatsToDatastore() { private long webhookRetryDelay = 1000; /** - * Enable webhook authentication for webrtc. play + * Webhook webrtc play authentication url. */ - @Value("${webhookPlayAuthEnabled:false}") - private boolean webhookPlayAuthEnabled = false; + @Value("${webhookPlayAuthUrl:#{null}}") + private String webhookPlayAuthUrl; - /** - * Enable webhook authentication for publish. - */ - @Value("${webhookPublishAuthEnabled:false}") - private boolean webhookPublishAuthEnabled = false; public void setWriteStatsToDatastore(boolean writeStatsToDatastore) { this.writeStatsToDatastore = writeStatsToDatastore; @@ -3678,19 +3673,15 @@ public void setWebhookRetryDelay(long webhookRetryDelay) { } public boolean isWebhookPlayAuthEnabled() { - return webhookPlayAuthEnabled; - } - - public void setWebhookPlayAuthEnabled(boolean webhookPlayAuthEnabled) { - this.webhookPlayAuthEnabled = webhookPlayAuthEnabled; + return getWebhookPlayAuthUrl() != null && !getWebhookPlayAuthUrl().isEmpty(); } - public boolean isWebhookPublishAuthEnabled() { - return webhookPublishAuthEnabled; + public String getWebhookPlayAuthUrl() { + return webhookPlayAuthUrl; } - public void setWebhookPublishAuthEnabled(boolean webhookPublishAuthEnabled) { - this.webhookPublishAuthEnabled = webhookPublishAuthEnabled; + public void setWebhookPlayAuthUrl(String webhookPlayAuthUrl) { + this.webhookPlayAuthUrl = webhookPlayAuthUrl; } } diff --git a/src/main/java/io/antmedia/security/AcceptOnlyStreamsWithWebhook.java b/src/main/java/io/antmedia/security/AcceptOnlyStreamsWithWebhook.java index edd91ff2c..532f2976c 100644 --- a/src/main/java/io/antmedia/security/AcceptOnlyStreamsWithWebhook.java +++ b/src/main/java/io/antmedia/security/AcceptOnlyStreamsWithWebhook.java @@ -41,9 +41,8 @@ public synchronized boolean isPublishAllowed(IScope scope, String streamId, Stri if (appSettings == null){ appSettings = (AppSettings) scope.getContext().getBean(AppSettings.BEAN_NAME); } - final String webhookAuthURL = appSettings.getWebhookAuthenticateURL(); - boolean publishWebhookAuthEnabled = appSettings.isWebhookPublishAuthEnabled(); - if (publishWebhookAuthEnabled && webhookAuthURL != null && !webhookAuthURL.isEmpty()) + final String publishWebhookAuthURL = appSettings.getWebhookAuthenticateURL(); + if (publishWebhookAuthURL != null && !publishWebhookAuthURL.isEmpty()) { try (CloseableHttpClient client = getHttpClient()) { @@ -62,7 +61,7 @@ public synchronized boolean isPublishAllowed(IScope scope, String streamId, Stri RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(2 * 1000).setSocketTimeout(5*1000).build(); - HttpRequestBase post = (HttpRequestBase) RequestBuilder.post().setUri(webhookAuthURL) + HttpRequestBase post = (HttpRequestBase) RequestBuilder.post().setUri(publishWebhookAuthURL) .setHeader(HttpHeaders.CONTENT_TYPE, "application/json") .setEntity(new StringEntity(instance.toString())).build(); post.setConfig(requestConfig); diff --git a/src/test/java/io/antmedia/test/AppSettingsUnitTest.java b/src/test/java/io/antmedia/test/AppSettingsUnitTest.java index 0bf2a3033..083485082 100644 --- a/src/test/java/io/antmedia/test/AppSettingsUnitTest.java +++ b/src/test/java/io/antmedia/test/AppSettingsUnitTest.java @@ -313,6 +313,10 @@ public void testSettings() { appSettings.setWebhookRetryDelay(webHookRetryDelay); assertEquals(webHookRetryDelay, appSettings.getWebhookRetryDelay()); + String webhookPlayAuthUrl = "playAuthUrl"; + appSettings.setWebhookPlayAuthUrl(webhookPlayAuthUrl); + assertEquals(webhookPlayAuthUrl, appSettings.getWebhookPlayAuthUrl()); + } @@ -538,15 +542,15 @@ public void testUnsetAppSettings(AppSettings appSettings) { assertEquals(0, appSettings.getWebhookRetryCount()); assertEquals(1000, appSettings.getWebhookRetryDelay()); - assertFalse(appSettings.isWebhookPublishAuthEnabled()); assertFalse(appSettings.isWebhookPlayAuthEnabled()); + assertNull(appSettings.getWebhookPlayAuthUrl()); //if we add a new field, we just need to check its default value in this test //When a new field is added or removed please update the number of fields and make this test pass //by also checking its default value. assertEquals("New field is added to settings. PAY ATTENTION: Please CHECK ITS DEFAULT VALUE and fix the number of fields.", - 179, numberOfFields); + 178, numberOfFields); } diff --git a/src/test/java/io/antmedia/test/security/AcceptOnlyStreamsWithWebhookTest.java b/src/test/java/io/antmedia/test/security/AcceptOnlyStreamsWithWebhookTest.java index 35d58886d..cff0b09dd 100644 --- a/src/test/java/io/antmedia/test/security/AcceptOnlyStreamsWithWebhookTest.java +++ b/src/test/java/io/antmedia/test/security/AcceptOnlyStreamsWithWebhookTest.java @@ -44,7 +44,6 @@ public void testAcceptOnlyStreamsWithWebhook() AppSettings appSettings = new AppSettings(); appSettings.setWebhookAuthenticateURL("sampleurl"); - appSettings.setWebhookPublishAuthEnabled(true); filter.setAppSettings(appSettings); boolean publishAllowed = filter.isPublishAllowed(scope, "streamId", "mode", null, null); assertFalse(publishAllowed); @@ -57,7 +56,6 @@ public void testIsPublishAllowedWithWebhook() throws IOException { IScope scope = Mockito.mock(IScope.class); AppSettings appSettings = new AppSettings(); appSettings.setWebhookAuthenticateURL("asd"); - appSettings.setWebhookPublishAuthEnabled(true); InMemoryDataStore dataStore = new InMemoryDataStore("db"); DataStoreFactory factory = Mockito.mock(DataStoreFactory.class); From e4f886e76057d5dff03582366fe7227087b1f601 Mon Sep 17 00:00:00 2001 From: lastpeony Date: Thu, 2 May 2024 11:26:05 +0300 Subject: [PATCH 3/5] unittest fix --- src/test/java/io/antmedia/test/AppSettingsUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/antmedia/test/AppSettingsUnitTest.java b/src/test/java/io/antmedia/test/AppSettingsUnitTest.java index 02c7ab41a..99fd6b043 100644 --- a/src/test/java/io/antmedia/test/AppSettingsUnitTest.java +++ b/src/test/java/io/antmedia/test/AppSettingsUnitTest.java @@ -551,7 +551,7 @@ public void testUnsetAppSettings(AppSettings appSettings) { //by also checking its default value. assertEquals("New field is added to settings. PAY ATTENTION: Please CHECK ITS DEFAULT VALUE and fix the number of fields.", - 178, numberOfFields); + 179, numberOfFields); } From 0cae9f6c3dca66b5a834b08f1e1e2fc3678ea487 Mon Sep 17 00:00:00 2001 From: lastpeony Date: Mon, 13 May 2024 10:42:21 +0300 Subject: [PATCH 4/5] fix unittest --- src/test/java/io/antmedia/test/AppSettingsUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/antmedia/test/AppSettingsUnitTest.java b/src/test/java/io/antmedia/test/AppSettingsUnitTest.java index 0f5fd271c..6442de112 100644 --- a/src/test/java/io/antmedia/test/AppSettingsUnitTest.java +++ b/src/test/java/io/antmedia/test/AppSettingsUnitTest.java @@ -558,7 +558,7 @@ public void testUnsetAppSettings(AppSettings appSettings) { //by also checking its default value. assertEquals("New field is added to settings. PAY ATTENTION: Please CHECK ITS DEFAULT VALUE and fix the number of fields.", - 179, numberOfFields); + 180, numberOfFields); } From b22578824fba08049f50741e24e6829c8a169fba Mon Sep 17 00:00:00 2001 From: lastpeony Date: Wed, 15 May 2024 15:55:35 +0300 Subject: [PATCH 5/5] fix app settings unit test --- src/test/java/io/antmedia/test/AppSettingsUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/antmedia/test/AppSettingsUnitTest.java b/src/test/java/io/antmedia/test/AppSettingsUnitTest.java index 51316d93f..8690f43be 100644 --- a/src/test/java/io/antmedia/test/AppSettingsUnitTest.java +++ b/src/test/java/io/antmedia/test/AppSettingsUnitTest.java @@ -561,7 +561,7 @@ public void testUnsetAppSettings(AppSettings appSettings) { //by also checking its default value. assertEquals("New field is added to settings. PAY ATTENTION: Please CHECK ITS DEFAULT VALUE and fix the number of fields.", - 180, numberOfFields); + 181, numberOfFields); }