cloud_sql_ruby_connector 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4d38ffcda4c30104ba07a59ea00720295ba130af8141a729893d950b08e5d093
4
+ data.tar.gz: c6e4aff3744c11c910f7baf63d806a0985f629c5a218b1d654f6c909311805e7
5
+ SHA512:
6
+ metadata.gz: b8d86ef87df76af19648379855d68187de2a3c469a72545cad59dd17f1703a8eb3c7c0dfd8fad59f9f0d8eb6b87e4f340ec885a7f3fdcbd0b037a58fdc0b08c5
7
+ data.tar.gz: e6712e2d7aaeeb6d80b0f2e92846da0fd64a673f1e79ccf4c346303e6b4c8ca44be12653c9dbacee0f25f691a4051a9dcf1a20563695d10f5b6c9e62e88a978e
data/CHANGELOG.md ADDED
@@ -0,0 +1,22 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [1.0.0] - 2026-01-17
11
+
12
+ ### Added
13
+
14
+ - Initial public release with `CloudSQLRubyConnector::PostgreSQL::Connector`
15
+ - Support for PostgreSQL connections via Cloud SQL
16
+ - Support for PUBLIC, PRIVATE, and PSC IP address types
17
+ - Support for PASSWORD (built-in) and IAM database authentication
18
+ - Automatic ephemeral certificate management with refresh before expiration
19
+ - Multiple credential sources: service account, user credentials, and GCE/Cloud Run metadata server
20
+ - Thread-safe connection handling with TLS 1.3 SSL proxy
21
+ - Rails integration with custom `cloud_sql_postgresql` adapter
22
+ - Input validation for credentials and connection parameters
@@ -0,0 +1,43 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project,
4
+ and in the interest of fostering an open and welcoming community,
5
+ we pledge to respect all people who contribute through reporting issues,
6
+ posting feature requests, updating documentation,
7
+ submitting pull requests or patches, and other activities.
8
+
9
+ We are committed to making participation in this project
10
+ a harassment-free experience for everyone,
11
+ regardless of level of experience, gender, gender identity and expression,
12
+ sexual orientation, disability, personal appearance,
13
+ body size, race, ethnicity, age, religion, or nationality.
14
+
15
+ Examples of unacceptable behavior by participants include:
16
+
17
+ * The use of sexualized language or imagery
18
+ * Personal attacks
19
+ * Trolling or insulting/derogatory comments
20
+ * Public or private harassment
21
+ * Publishing other's private information,
22
+ such as physical or electronic
23
+ addresses, without explicit permission
24
+ * Other unethical or unprofessional conduct.
25
+
26
+ Project maintainers have the right and responsibility to remove, edit, or reject
27
+ comments, commits, code, wiki edits, issues, and other contributions
28
+ that are not aligned to this Code of Conduct.
29
+ By adopting this Code of Conduct,
30
+ project maintainers commit themselves to fairly and consistently
31
+ applying these principles to every aspect of managing this project.
32
+ Project maintainers who do not follow or enforce the Code of Conduct
33
+ may be permanently removed from the project team.
34
+
35
+ This code of conduct applies both within project spaces and in public spaces
36
+ when an individual is representing the project or its community.
37
+
38
+ Instances of abusive, harassing, or otherwise unacceptable behavior
39
+ may be reported by opening an issue
40
+ or contacting one or more of the project maintainers.
41
+
42
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0,
43
+ available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,49 @@
1
+ # How to Contribute
2
+
3
+ We'd love to accept your patches and contributions to this project. There are
4
+ just a few small guidelines you need to follow.
5
+
6
+ ## Code Reviews
7
+
8
+ All submissions, including submissions by project members, require review. We
9
+ use GitHub pull requests for this purpose. Consult
10
+ [GitHub Help](https://help.github.com/articles/about-pull-requests/) for more
11
+ information on using pull requests.
12
+
13
+ ## Development Setup
14
+
15
+ 1. Fork and clone the repository
16
+ 2. Install dependencies: `bundle install`
17
+ 3. Run tests: `bundle exec rake test`
18
+ 4. Run linter: `bundle exec rubocop`
19
+
20
+ ## Running Tests
21
+
22
+ ```sh
23
+ bundle exec rake test
24
+ ```
25
+
26
+ Or run both tests and linter:
27
+
28
+ ```sh
29
+ bundle exec rake
30
+ ```
31
+
32
+ ## Code Style
33
+
34
+ This project uses RuboCop for code style enforcement. Run the linter before submitting:
35
+
36
+ ```sh
37
+ bundle exec rubocop
38
+ ```
39
+
40
+ ## Commit Messages
41
+
42
+ - Use the present tense ("Add feature" not "Added feature")
43
+ - Use the imperative mood ("Move cursor to..." not "Moves cursor to...")
44
+ - Limit the first line to 72 characters or less
45
+ - Reference issues and pull requests after the first line
46
+
47
+ ## Community Guidelines
48
+
49
+ This project follows the [Contributor Covenant Code of Conduct](CODE_OF_CONDUCT.md).
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,233 @@
1
+ # Cloud SQL Ruby Connector
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/cloud_sql_ruby_connector.svg)](https://badge.fury.io/rb/cloud_sql_ruby_connector)
4
+
5
+ An unofficial Ruby connector for Google Cloud SQL that provides secure connections without requiring the Cloud SQL Auth Proxy.
6
+
7
+ > **⚠️ Unofficial Library:** This is a community-maintained Ruby implementation, not an official Google product. For official connectors, see:
8
+ > - [cloud-sql-nodejs-connector](https://github.com/GoogleCloudPlatform/cloud-sql-nodejs-connector) (Node.js)
9
+ > - [cloud-sql-python-connector](https://github.com/GoogleCloudPlatform/cloud-sql-python-connector) (Python)
10
+ > - [cloud-sql-go-connector](https://github.com/GoogleCloudPlatform/cloud-sql-go-connector) (Go)
11
+
12
+ ## Features
13
+
14
+ - **Secure Connections:** TLS 1.3 encryption with automatic certificate management
15
+ - **Multiple Auth Methods:** Supports both built-in database authentication (username/password) and IAM database authentication
16
+ - **Flexible Networking:** Connect via public IP, private IP, or Private Service Connect (PSC)
17
+ - **No Proxy Required:** Direct secure connections without running Cloud SQL Auth Proxy as a separate process
18
+ - **Automatic Credentials:** On GCE/Cloud Run, uses the metadata server automatically - no credential files needed
19
+ - **No External Dependencies:** Uses only Ruby's built-in libraries (openssl, net/http, json, socket)
20
+
21
+ ## Supported Databases
22
+
23
+ Currently supported:
24
+
25
+ - PostgreSQL (via [`pg`](https://rubygems.org/gems/pg) gem)
26
+
27
+ ## Installation
28
+
29
+ Add this line to your application's Gemfile:
30
+
31
+ ```ruby
32
+ gem 'cloud_sql_ruby_connector'
33
+ ```
34
+
35
+ And then execute:
36
+
37
+ ```sh
38
+ bundle install
39
+ ```
40
+
41
+ Or install it yourself as:
42
+
43
+ ```sh
44
+ gem install cloud_sql_ruby_connector
45
+ ```
46
+
47
+ ## Prerequisites
48
+
49
+ - Ruby >= 3.3
50
+ - [Cloud SQL Admin API](https://console.cloud.google.com/apis/api/sqladmin.googleapis.com) enabled in your Google Cloud Project
51
+ - The calling identity must have the [Cloud SQL Client](https://cloud.google.com/sql/docs/mysql/roles-and-permissions) role (or equivalent permissions) to fetch instance metadata and certificates
52
+
53
+ ## Credentials
54
+
55
+ The connector needs Google Cloud credentials to fetch instance metadata and certificates from the Cloud SQL Admin API.
56
+
57
+ **On GCE, Cloud Run, GKE, or other Google Cloud environments:**
58
+ No configuration needed - the connector automatically uses the metadata server.
59
+
60
+ **Outside Google Cloud:**
61
+ Use one of these methods:
62
+ 1. Set `GOOGLE_APPLICATION_CREDENTIALS` environment variable to a service account JSON key file path
63
+ 2. Run `gcloud auth application-default login` to use your user credentials
64
+
65
+ ## Usage
66
+
67
+ ### Basic Usage with PostgreSQL
68
+
69
+ ```ruby
70
+ require 'cloud_sql_ruby_connector'
71
+ require 'pg'
72
+
73
+ connector = CloudSQLRubyConnector::PostgreSQL::Connector.new("my-project:us-central1:my-instance")
74
+ conn = connector.connect(
75
+ user: "my-user",
76
+ password: "my-password",
77
+ dbname: "my-database"
78
+ )
79
+
80
+ result = conn.exec("SELECT NOW()")
81
+ puts result.first
82
+
83
+ conn.close
84
+ connector.close
85
+ ```
86
+
87
+ ### Specifying IP Address Type
88
+
89
+ The connector supports `:public` (default), `:private`, and `:psc` IP address types:
90
+
91
+ ```ruby
92
+ # Using Private IP
93
+ connector = CloudSQLRubyConnector::PostgreSQL::Connector.new(
94
+ "my-project:us-central1:my-instance",
95
+ ip_type: :private
96
+ )
97
+
98
+ # Using Private Service Connect (PSC)
99
+ connector = CloudSQLRubyConnector::PostgreSQL::Connector.new(
100
+ "my-project:us-central1:my-instance",
101
+ ip_type: :psc
102
+ )
103
+ ```
104
+
105
+ **Note:** When using Private IP or PSC, your application must be connected to the appropriate VPC network.
106
+
107
+ ### IAM Database Authentication
108
+
109
+ For IAM database authentication, configure your Cloud SQL instance to allow IAM authentication and add an IAM database user:
110
+
111
+ ```ruby
112
+ connector = CloudSQLRubyConnector::PostgreSQL::Connector.new(
113
+ "my-project:us-central1:my-instance",
114
+ ip_type: :private,
115
+ auth_type: :iam
116
+ )
117
+
118
+ # For service accounts, use the full email - the connector strips .gserviceaccount.com automatically
119
+ conn = connector.connect(
120
+ user: "my-service-account@my-project.iam.gserviceaccount.com",
121
+ dbname: "my-database"
122
+ )
123
+ # Note: password is not required for IAM authentication
124
+ ```
125
+
126
+ ### Using with Rails
127
+
128
+ Create an initializer at `config/initializers/cloud_sql.rb`:
129
+
130
+ ```ruby
131
+ require 'cloud_sql_ruby_connector/rails'
132
+
133
+ CloudSQLRubyConnector::Rails.setup!(
134
+ instance: ENV['CLOUD_SQL_INSTANCE'], # e.g., "my-project:us-central1:my-instance"
135
+ ip_type: :private, # :public, :private, or :psc
136
+ auth_type: :iam # :password or :iam
137
+ )
138
+ ```
139
+
140
+ Then in `config/database.yml`:
141
+
142
+ ```yaml
143
+ # With IAM authentication
144
+ production:
145
+ adapter: cloud_sql_postgresql
146
+ database: my_database
147
+ username: my-sa@my-project.iam.gserviceaccount.com
148
+ pool: 5
149
+
150
+ # With password authentication
151
+ production:
152
+ adapter: cloud_sql_postgresql
153
+ database: my_database
154
+ username: my_user
155
+ password: <%= ENV['DB_PASSWORD'] %>
156
+ pool: 5
157
+ ```
158
+
159
+ ## API Reference
160
+
161
+ ### CloudSQLRubyConnector::PostgreSQL::Connector
162
+
163
+ #### `#initialize(instance_connection_name, **options)`
164
+
165
+ Creates a new connector instance.
166
+
167
+ **Parameters:**
168
+ - `instance_connection_name` (String): Cloud SQL instance connection name in format `PROJECT:REGION:INSTANCE`
169
+ - `credentials` (Credentials::Base, optional): Custom credentials object
170
+ - `ip_type` (Symbol/String, optional): IP address type - `:public` (default), `:private`, or `:psc`
171
+ - `auth_type` (Symbol/String, optional): Authentication type - `:password` (default) or `:iam`
172
+ - `api_endpoint` (String, optional): Custom Cloud SQL Admin API endpoint
173
+
174
+ #### `#connect(user:, dbname:, password: nil, **options)`
175
+
176
+ Creates a connected `PG::Connection`.
177
+
178
+ **Parameters:**
179
+ - `user` (String): Database username
180
+ - `dbname` (String): Database name
181
+ - `password` (String, optional): Database password (not required for IAM auth)
182
+ - Additional options are passed to `PG.connect`
183
+
184
+ **Returns:** `PG::Connection`
185
+
186
+ #### `#get_options`
187
+
188
+ Returns connection options that can be used with `PG.connect`.
189
+
190
+ **Returns:** Hash with `:stream`, `:ip_address`, `:server_ca_cert`, `:client_cert`, `:private_key`
191
+
192
+ #### `#ip_address`
193
+
194
+ Returns the IP address of the Cloud SQL instance.
195
+
196
+ **Returns:** String
197
+
198
+ #### `#close`
199
+
200
+ Closes the connector and cleans up resources.
201
+
202
+ ### IP Address Types
203
+
204
+ Use symbols (recommended) or constants:
205
+
206
+ | Symbol | Constant | Description |
207
+ |--------|----------|-------------|
208
+ | `:public` | `IpAddressTypes::PUBLIC` | Connect via public IP (default) |
209
+ | `:private` | `IpAddressTypes::PRIVATE` | Connect via private IP (requires VPC) |
210
+ | `:psc` | `IpAddressTypes::PSC` | Connect via Private Service Connect |
211
+
212
+ ### Authentication Types
213
+
214
+ | Symbol | Constant | Description |
215
+ |--------|----------|-------------|
216
+ | `:password` | `AuthTypes::PASSWORD` | Built-in database authentication (default) |
217
+ | `:iam` | `AuthTypes::IAM` | IAM database authentication |
218
+
219
+ ## Contributing
220
+
221
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for details.
222
+
223
+ ## Code of Conduct
224
+
225
+ See [CODE_OF_CONDUCT.md](CODE_OF_CONDUCT.md) for details.
226
+
227
+ ## Security
228
+
229
+ See [SECURITY.md](SECURITY.md) for reporting vulnerabilities.
230
+
231
+ ## License
232
+
233
+ Apache License 2.0 - see [LICENSE](LICENSE) for details.
data/SECURITY.md ADDED
@@ -0,0 +1,11 @@
1
+ # Security Policy
2
+
3
+ ## Reporting a Vulnerability
4
+
5
+ If you discover a security vulnerability, please report it by opening a
6
+ [security advisory](https://github.com/your-username/cloud-sql-ruby-connector/security/advisories/new)
7
+ on GitHub.
8
+
9
+ Please do not report security vulnerabilities through public GitHub issues.
10
+
11
+ We will respond within 5 working days of your report.
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2026 Martin Milo
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module CloudSQLRubyConnector
18
+ # Authentication types for Cloud SQL connections
19
+ module AuthTypes
20
+ # Built-in database authentication (username/password)
21
+ PASSWORD = "PASSWORD"
22
+
23
+ # IAM database authentication
24
+ IAM = "IAM"
25
+
26
+ ALL = [PASSWORD, IAM].freeze
27
+
28
+ class << self
29
+ def valid?(type)
30
+ ALL.include?(type.to_s.upcase)
31
+ end
32
+
33
+ def normalize(type)
34
+ normalized = type.to_s.upcase
35
+ raise ConfigurationError, "Invalid auth type: #{type}. Valid types: #{ALL.join(", ")}" unless valid?(normalized)
36
+
37
+ normalized
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright 2026 Martin Milo
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # https://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ module CloudSQLRubyConnector
18
+ module Credentials
19
+ # Base class for all credential types
20
+ class Base
21
+ SCOPES = {
22
+ admin: "https://www.googleapis.com/auth/sqlservice.admin",
23
+ login: "https://www.googleapis.com/auth/sqlservice.login"
24
+ }.freeze
25
+
26
+ TOKEN_URI = "https://oauth2.googleapis.com/token"
27
+
28
+ def initialize
29
+ @tokens = {}
30
+ @token_expiries = {}
31
+ @mutex = Mutex.new
32
+ end
33
+
34
+ # Get an access token for the specified scope
35
+ # @param scope [Symbol] :admin or :login
36
+ # @return [String] the access token
37
+ def access_token(scope: :admin)
38
+ @mutex.synchronize do
39
+ refresh_token(scope) if token_expired?(scope)
40
+ @tokens[scope]
41
+ end
42
+ end
43
+
44
+ protected
45
+
46
+ def token_expired?(scope)
47
+ @tokens[scope].nil? || @token_expiries[scope].nil? || Time.now >= @token_expiries[scope]
48
+ end
49
+
50
+ def refresh_token(scope)
51
+ raise NotImplementedError, "Subclasses must implement #refresh_token"
52
+ end
53
+
54
+ def store_token(scope, token, expires_in)
55
+ @tokens[scope] = token
56
+ # Refresh 60 seconds before actual expiration
57
+ @token_expiries[scope] = Time.now + expires_in.to_i - 60
58
+ end
59
+ end
60
+ end
61
+ end