From 70c045466944a02d1ed22c3538a3e6d80739a644 Mon Sep 17 00:00:00 2001 From: takuya fujiwara Date: Tue, 11 Jun 2019 08:54:57 +0900 Subject: [PATCH 1/4] add cloud storage function. --- .../FirebaseAdmin.IntegrationTests.csproj | 5 +- .../IntegrationTestUtils.cs | 5 ++ .../StorageClientHelperTest.cs | 79 +++++++++++++++++ .../FirebaseAdmin.Snippets.csproj | 1 + .../Cloud/StorageClientHelperTest.cs | 85 +++++++++++++++++++ .../FirebaseAdmin.Tests.csproj | 1 + .../Cloud/StorageClientHelper.cs | 82 ++++++++++++++++++ .../FirebaseAdmin/FirebaseAdmin.csproj | 1 + FirebaseAdmin/FirebaseAdmin/FirebaseApp.cs | 6 ++ 9 files changed, 264 insertions(+), 1 deletion(-) create mode 100644 FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs create mode 100644 FirebaseAdmin/FirebaseAdmin.Tests/Cloud/StorageClientHelperTest.cs create mode 100644 FirebaseAdmin/FirebaseAdmin/Cloud/StorageClientHelper.cs diff --git a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseAdmin.IntegrationTests.csproj b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseAdmin.IntegrationTests.csproj index d146599a..3e3a6f55 100644 --- a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseAdmin.IntegrationTests.csproj +++ b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/FirebaseAdmin.IntegrationTests.csproj @@ -1,14 +1,17 @@ - + netcoreapp2.0 false + ../../FirebaseAdmin.snk + true true ../../stylecop_test.ruleset + diff --git a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/IntegrationTestUtils.cs b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/IntegrationTestUtils.cs index bee0adc0..b7558c55 100644 --- a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/IntegrationTestUtils.cs +++ b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/IntegrationTestUtils.cs @@ -45,5 +45,10 @@ public static string GetApiKey() { return System.IO.File.ReadAllText(ApiKeyFile).Trim(); } + + public static string GetDefaultBucketName(string projectId) + { + return projectId + ".appspot.com"; + } } } diff --git a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs new file mode 100644 index 00000000..ff1e921a --- /dev/null +++ b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs @@ -0,0 +1,79 @@ +// Copyright 2018, Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using FirebaseAdmin.Cloud; +using Google.Cloud.Storage.V1; +using Xunit; + +namespace FirebaseAdmin.IntegrationTests +{ + public class StorageClientHelperTest + { + public StorageClientHelperTest() + { + IntegrationTestUtils.EnsureDefaultApp(); + } + + [Fact] + public void UseBucket() + { + var storageClient = StorageClientHelper.GetStorageClient(); + this.TestBucket(FirebaseApp.DefaultInstance.GetProjectId(), storageClient); + } + + [Fact] + public void UseBucketWithCustomEncryptionKey() + { + var app = FirebaseApp.Create(FirebaseApp.DefaultInstance.Options, "CustomEncryptionApp"); + try + { + EncryptionKey encryptionKey = EncryptionKey.Generate(); + var storageClient = StorageClientHelper.GetStorageClient(app, encryptionKey); + Assert.Equal(encryptionKey, storageClient.EncryptionKey); + this.TestBucket(app.GetProjectId(), storageClient); + } + finally + { + app.Delete(); + } + } + + private void TestBucket(string projectId, StorageClient storageClient) + { + var bucketName = IntegrationTestUtils.GetDefaultBucketName(projectId); + + var fileName = "FirebaseStorageTest.txt"; + var content = "FirebaseStorageTest"; + using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content))) + { + var obj1 = storageClient.UploadObject(bucketName, fileName, "text/plain", stream); + Assert.Equal(bucketName, obj1.Bucket); + } + + using (var stream = new MemoryStream()) + { + storageClient.DownloadObject(bucketName, fileName, stream); + Assert.Equal(content, Encoding.UTF8.GetString(stream.ToArray())); + } + + storageClient.DeleteObject(bucketName, fileName); + } + } +} \ No newline at end of file diff --git a/FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAdmin.Snippets.csproj b/FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAdmin.Snippets.csproj index ee80a864..d0f9e34c 100644 --- a/FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAdmin.Snippets.csproj +++ b/FirebaseAdmin/FirebaseAdmin.Snippets/FirebaseAdmin.Snippets.csproj @@ -9,6 +9,7 @@ + diff --git a/FirebaseAdmin/FirebaseAdmin.Tests/Cloud/StorageClientHelperTest.cs b/FirebaseAdmin/FirebaseAdmin.Tests/Cloud/StorageClientHelperTest.cs new file mode 100644 index 00000000..31d2267c --- /dev/null +++ b/FirebaseAdmin/FirebaseAdmin.Tests/Cloud/StorageClientHelperTest.cs @@ -0,0 +1,85 @@ +// Copyright 2018, Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.IO; +using System.Text; +using System.Threading; +using System.Threading.Tasks; +using FirebaseAdmin.Cloud; +using Google.Apis.Auth.OAuth2; +using Google.Cloud.Storage.V1; +using Xunit; + +namespace FirebaseAdmin.Cloud.Tests +{ + public class StorageClientHelperTest : IDisposable + { + private static readonly GoogleCredential MockCredential = + GoogleCredential.FromAccessToken("test-token"); + + [Fact] + public void GetStorageClientWithoutApp() + { + Assert.Null(StorageClientHelper.GetStorageClient()); + } + + [Fact] + public void GetDefaultStorageClient() + { + var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential }); + StorageClient storageClient = StorageClientHelper.GetStorageClient(); + Assert.Same(storageClient, StorageClientHelper.GetStorageClient()); + app.Delete(); + Assert.Null(StorageClientHelper.GetStorageClient()); + } + + [Fact] + public void GetStorageClient() + { + var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential }); + StorageClient storageClient = StorageClientHelper.GetStorageClient(app); + Assert.Same(storageClient, StorageClientHelper.GetStorageClient(app)); + app.Delete(); + Assert.Throws(() => StorageClientHelper.GetStorageClient(app)); + } + + [Fact] + public void GetStorageClientWithEncryptionKey() + { + var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential }); + EncryptionKey encryptionKey = EncryptionKey.Generate(); + StorageClient storageClient = StorageClientHelper.GetStorageClient(app, encryptionKey); + Assert.Same(storageClient, StorageClientHelper.GetStorageClient(app, encryptionKey)); + app.Delete(); + Assert.Throws(() => StorageClientHelper.GetStorageClient(app, encryptionKey)); + } + + [Fact] + public void UseAfterDelete() + { + var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential }); + StorageClient storageClient = StorageClientHelper.GetStorageClient(app); + app.Delete(); + Assert.Throws( + () => storageClient.GetBucket("test")); + } + + public void Dispose() + { + FirebaseApp.DeleteAll(); + } + } +} diff --git a/FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAdmin.Tests.csproj b/FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAdmin.Tests.csproj index 8641fa7b..5cacebeb 100644 --- a/FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAdmin.Tests.csproj +++ b/FirebaseAdmin/FirebaseAdmin.Tests/FirebaseAdmin.Tests.csproj @@ -11,6 +11,7 @@ + diff --git a/FirebaseAdmin/FirebaseAdmin/Cloud/StorageClientHelper.cs b/FirebaseAdmin/FirebaseAdmin/Cloud/StorageClientHelper.cs new file mode 100644 index 00000000..0ac09392 --- /dev/null +++ b/FirebaseAdmin/FirebaseAdmin/Cloud/StorageClientHelper.cs @@ -0,0 +1,82 @@ +// Copyright 2018, Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +using System; +using System.Collections.Generic; +using System.Text; +using Google.Cloud.Storage.V1; + +namespace FirebaseAdmin.Cloud +{ + /// + /// StorageClientHelper provides access to Google Cloud Storage APIs. You + /// can get an instance of this class via StorageClientHelper.GetStorageClient(). + /// + public sealed class StorageClientHelper : IFirebaseService + { + private StorageClient storageClient; + + private StorageClientHelper(FirebaseApp app, EncryptionKey encryptionKey = null) + { + this.storageClient = StorageClient.Create(app.Options.Credential, encryptionKey); + } + + /// + /// Gets the StorageClient instance associated with the default Firebase app. Return value is + /// null if the default app doesn't yet exist. + /// + /// Optional EncryptionKey to use for all relevant object-based operations by default. May be null. + /// The instance associated with the specified + /// app. + public static StorageClient GetStorageClient(EncryptionKey encryptionKey = null) + { + var app = FirebaseApp.DefaultInstance; + if (app == null) + { + return null; + } + + return GetStorageClient(app, encryptionKey); + } + + /// + /// Returns the StorageClient instance for the specified app. + /// + /// The instance associated with the specified + /// app. + /// If the app argument is null. + /// An app instance. + /// Optional EncryptionKey to use for all relevant object-based operations by default. May be null. + public static StorageClient GetStorageClient(FirebaseApp app, EncryptionKey encryptionKey = null) + { + if (app == null) + { + throw new ArgumentNullException("App argument must not be null."); + } + + return app.GetOrInit(typeof(StorageClientHelper).Name, () => + { + return new StorageClientHelper(app, encryptionKey); + }).storageClient; + } + + /// + /// Deletes this service instance. + /// + void IFirebaseService.Delete() + { + this.storageClient.Dispose(); + } + } +} diff --git a/FirebaseAdmin/FirebaseAdmin/FirebaseAdmin.csproj b/FirebaseAdmin/FirebaseAdmin/FirebaseAdmin.csproj index 876a66f9..e15a8207 100644 --- a/FirebaseAdmin/FirebaseAdmin/FirebaseAdmin.csproj +++ b/FirebaseAdmin/FirebaseAdmin/FirebaseAdmin.csproj @@ -26,6 +26,7 @@ + all diff --git a/FirebaseAdmin/FirebaseAdmin/FirebaseApp.cs b/FirebaseAdmin/FirebaseAdmin/FirebaseApp.cs index 0a3544f2..8d155649 100644 --- a/FirebaseAdmin/FirebaseAdmin/FirebaseApp.cs +++ b/FirebaseAdmin/FirebaseAdmin/FirebaseApp.cs @@ -28,6 +28,12 @@ "3003684e85e61cf15f13150008c81f0b75a252673028e530ea95d0c581378da8c6846526ab9597" + "4c6d0bc66d2462b51af69968a0e25114bde8811e0d6ee1dc22d4a59eee6a8bba4712cba839652f" + "badddb9c")] +[assembly: InternalsVisibleToAttribute("FirebaseAdmin.IntegrationTests,PublicKey=" + +"002400000480000094000000060200000024000052534131000400000100010081328559eaab41" + +"055b84af73469863499d81625dcbba8d8decb298b69e0f783a0958cf471fd4f76327b85a7d4b02" + +"3003684e85e61cf15f13150008c81f0b75a252673028e530ea95d0c581378da8c6846526ab9597" + +"4c6d0bc66d2462b51af69968a0e25114bde8811e0d6ee1dc22d4a59eee6a8bba4712cba839652f" + +"badddb9c")] namespace FirebaseAdmin { internal delegate TResult ServiceFactory() From 4fc783685a06f35bce0702ecf059aae8952efc89 Mon Sep 17 00:00:00 2001 From: takuya fujiwara Date: Tue, 11 Jun 2019 22:14:12 +0900 Subject: [PATCH 2/4] delete encryption key. --- .../StorageClientHelperTest.cs | 21 ------------------- .../Cloud/StorageClientHelperTest.cs | 17 --------------- .../Cloud/StorageClientHelper.cs | 16 ++++++-------- 3 files changed, 6 insertions(+), 48 deletions(-) diff --git a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs index ff1e921a..79f44250 100644 --- a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs +++ b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs @@ -12,12 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -using System; -using System.Collections.Generic; using System.IO; using System.Text; -using System.Threading; -using System.Threading.Tasks; using FirebaseAdmin.Cloud; using Google.Cloud.Storage.V1; using Xunit; @@ -38,23 +34,6 @@ public void UseBucket() this.TestBucket(FirebaseApp.DefaultInstance.GetProjectId(), storageClient); } - [Fact] - public void UseBucketWithCustomEncryptionKey() - { - var app = FirebaseApp.Create(FirebaseApp.DefaultInstance.Options, "CustomEncryptionApp"); - try - { - EncryptionKey encryptionKey = EncryptionKey.Generate(); - var storageClient = StorageClientHelper.GetStorageClient(app, encryptionKey); - Assert.Equal(encryptionKey, storageClient.EncryptionKey); - this.TestBucket(app.GetProjectId(), storageClient); - } - finally - { - app.Delete(); - } - } - private void TestBucket(string projectId, StorageClient storageClient) { var bucketName = IntegrationTestUtils.GetDefaultBucketName(projectId); diff --git a/FirebaseAdmin/FirebaseAdmin.Tests/Cloud/StorageClientHelperTest.cs b/FirebaseAdmin/FirebaseAdmin.Tests/Cloud/StorageClientHelperTest.cs index 31d2267c..52c9d177 100644 --- a/FirebaseAdmin/FirebaseAdmin.Tests/Cloud/StorageClientHelperTest.cs +++ b/FirebaseAdmin/FirebaseAdmin.Tests/Cloud/StorageClientHelperTest.cs @@ -13,12 +13,6 @@ // limitations under the License. using System; -using System.Collections.Generic; -using System.IO; -using System.Text; -using System.Threading; -using System.Threading.Tasks; -using FirebaseAdmin.Cloud; using Google.Apis.Auth.OAuth2; using Google.Cloud.Storage.V1; using Xunit; @@ -56,17 +50,6 @@ public void GetStorageClient() Assert.Throws(() => StorageClientHelper.GetStorageClient(app)); } - [Fact] - public void GetStorageClientWithEncryptionKey() - { - var app = FirebaseApp.Create(new AppOptions() { Credential = MockCredential }); - EncryptionKey encryptionKey = EncryptionKey.Generate(); - StorageClient storageClient = StorageClientHelper.GetStorageClient(app, encryptionKey); - Assert.Same(storageClient, StorageClientHelper.GetStorageClient(app, encryptionKey)); - app.Delete(); - Assert.Throws(() => StorageClientHelper.GetStorageClient(app, encryptionKey)); - } - [Fact] public void UseAfterDelete() { diff --git a/FirebaseAdmin/FirebaseAdmin/Cloud/StorageClientHelper.cs b/FirebaseAdmin/FirebaseAdmin/Cloud/StorageClientHelper.cs index 0ac09392..b569414b 100644 --- a/FirebaseAdmin/FirebaseAdmin/Cloud/StorageClientHelper.cs +++ b/FirebaseAdmin/FirebaseAdmin/Cloud/StorageClientHelper.cs @@ -13,8 +13,6 @@ // limitations under the License. using System; -using System.Collections.Generic; -using System.Text; using Google.Cloud.Storage.V1; namespace FirebaseAdmin.Cloud @@ -27,19 +25,18 @@ public sealed class StorageClientHelper : IFirebaseService { private StorageClient storageClient; - private StorageClientHelper(FirebaseApp app, EncryptionKey encryptionKey = null) + private StorageClientHelper(FirebaseApp app) { - this.storageClient = StorageClient.Create(app.Options.Credential, encryptionKey); + this.storageClient = StorageClient.Create(app.Options.Credential); } /// /// Gets the StorageClient instance associated with the default Firebase app. Return value is /// null if the default app doesn't yet exist. /// - /// Optional EncryptionKey to use for all relevant object-based operations by default. May be null. /// The instance associated with the specified /// app. - public static StorageClient GetStorageClient(EncryptionKey encryptionKey = null) + public static StorageClient GetStorageClient() { var app = FirebaseApp.DefaultInstance; if (app == null) @@ -47,7 +44,7 @@ public static StorageClient GetStorageClient(EncryptionKey encryptionKey = null) return null; } - return GetStorageClient(app, encryptionKey); + return GetStorageClient(app); } /// @@ -57,8 +54,7 @@ public static StorageClient GetStorageClient(EncryptionKey encryptionKey = null) /// app. /// If the app argument is null. /// An app instance. - /// Optional EncryptionKey to use for all relevant object-based operations by default. May be null. - public static StorageClient GetStorageClient(FirebaseApp app, EncryptionKey encryptionKey = null) + public static StorageClient GetStorageClient(FirebaseApp app) { if (app == null) { @@ -67,7 +63,7 @@ public static StorageClient GetStorageClient(FirebaseApp app, EncryptionKey encr return app.GetOrInit(typeof(StorageClientHelper).Name, () => { - return new StorageClientHelper(app, encryptionKey); + return new StorageClientHelper(app); }).storageClient; } From b9caa172c5baf93d2861859b7cd36bfed44d670f Mon Sep 17 00:00:00 2001 From: takuya fujiwara Date: Tue, 11 Jun 2019 22:15:28 +0900 Subject: [PATCH 3/4] move GetDefaultBucketName() --- .../FirebaseAdmin.IntegrationTests/IntegrationTestUtils.cs | 5 ----- .../StorageClientHelperTest.cs | 7 ++++++- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/IntegrationTestUtils.cs b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/IntegrationTestUtils.cs index b7558c55..bee0adc0 100644 --- a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/IntegrationTestUtils.cs +++ b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/IntegrationTestUtils.cs @@ -45,10 +45,5 @@ public static string GetApiKey() { return System.IO.File.ReadAllText(ApiKeyFile).Trim(); } - - public static string GetDefaultBucketName(string projectId) - { - return projectId + ".appspot.com"; - } } } diff --git a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs index 79f44250..57921ba2 100644 --- a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs +++ b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs @@ -36,7 +36,7 @@ public void UseBucket() private void TestBucket(string projectId, StorageClient storageClient) { - var bucketName = IntegrationTestUtils.GetDefaultBucketName(projectId); + var bucketName = this.GetDefaultBucketName(projectId); var fileName = "FirebaseStorageTest.txt"; var content = "FirebaseStorageTest"; @@ -54,5 +54,10 @@ private void TestBucket(string projectId, StorageClient storageClient) storageClient.DeleteObject(bucketName, fileName); } + + private string GetDefaultBucketName(string projectId) + { + return projectId + ".appspot.com"; + } } } \ No newline at end of file From b496e211ab14896539694364739dc4a3ad304c94 Mon Sep 17 00:00:00 2001 From: takuya fujiwara Date: Tue, 11 Jun 2019 22:18:53 +0900 Subject: [PATCH 4/4] add new line at end of file. --- .../FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs index 57921ba2..377b79a5 100644 --- a/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs +++ b/FirebaseAdmin/FirebaseAdmin.IntegrationTests/StorageClientHelperTest.cs @@ -60,4 +60,4 @@ private string GetDefaultBucketName(string projectId) return projectId + ".appspot.com"; } } -} \ No newline at end of file +}