This project provides a .NET Standard library for creating and managing a secure connection to Google Cloud SQL instances using a local proxy. The proxy establishes an SSL/TLS connection to the Cloud SQL instance, allowing local applications to communicate with the database securely.
- Secure connection to Google Cloud SQL instances
- Automatic SSL/TLS certificate management
- Supports multiple concurrent connections
- Handles periodic certificate refresh
- .NET Standard 2.0 or later
- Google Cloud SDK
- Google Cloud SQL instance
To install the library, add it to your project via NuGet Package Manager:
PM> Install-Package Expert1.CloudSqlProxy
The proxy supports two methods of authentication:
- Credential File: Path to the Google credentials JSON file.
- JSON String: Google credentials JSON file content as a string.
To create and start a proxy instance, use the ProxyInstance.StartProxyAsync method. You can provide the authentication method, instance connection string, and credentials.
using System; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { string instance = "your-project:your-region:your-instance-id"; string credentialsPath = "path/to/your/credentials.json"; try { var proxyInstance = await ProxyInstance.StartProxyAsync( AuthenticationMethod.CredentialFile, instance, credentialsPath ); Console.WriteLine($"Proxy started. Connect to your database using DataSource: {proxyInstance.DataSource}"); // Use proxyInstance.DataSource to connect to your database // When done, dispose the instance to stop the proxy proxyInstance.Dispose(); } catch (Exception ex) { Console.WriteLine($"Failed to start proxy: {ex.Message}"); } } }Once the proxy is started, you can connect to your Cloud SQL database using the DataSource property of the ProxyInstance. This property provides the 127.0.0.1: string, which can be used in your database connection string.
For example, to connect to a SQL Server instance:
string connectionString = $"Server={proxyInstance.DataSource};Database=your-database;User Id=your-username;Password=your-password;"; using (var connection = new SqlConnection(connectionString)) { connection.Open(); // Perform database operations }To stop the proxy, you can call the Stop method or dispose of the ProxyInstance:
proxyInstance.Stop(); // Stops the proxy // Or, simply dispose the instance proxyInstance.Dispose();To stop all running proxies, use the InstanceManager.StopAllInstances method:
InstanceManager.StopAllInstances();