-
Notifications
You must be signed in to change notification settings - Fork 18
Integration Tests for TLS #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3e4abc0
Add TLS Integration tests
adam-fowler 09b5cfd
Add CI for TLS integration tests
adam-fowler c30a727
Set VALKEY_EXTRA_FLAGS env with extra parameters
adam-fowler de46514
Add database name
adam-fowler 3b73036
Move environment var setting
adam-fowler 80572c8
Add GH workspace to volume
adam-fowler a0f4588
Make key files readable by all
adam-fowler 95ddb6b
Re-instate all unit-tests
adam-fowler 67cd5b8
Fix ShellCheck error
adam-fowler a691b4d
Add extra help text to ensure users run generate-test-certs.sh
adam-fowler b7db3ff
Add REDIS_EXTRA_FLAGS env
adam-fowler dd69844
Disable TLS tests for Redis
adam-fowler 03690a3
Re-enable valkey
adam-fowler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,3 +12,4 @@ Package.resolved | |
| .benchmarkBaselines/ | ||
| .swift-version | ||
| .docc-build | ||
| valkey | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // | ||
| // This source file is part of the valkey-swift project | ||
| // Copyright (c) 2025 the valkey-swift project authors | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| import Foundation | ||
| import Logging | ||
| import NIOCore | ||
| import NIOSSL | ||
| import Testing | ||
| import Valkey | ||
|
|
||
| @testable import Valkey | ||
|
|
||
| @Suite( | ||
| "TLS Integration Tests", | ||
| .disabled(if: disableTLSTests != nil) | ||
| ) | ||
| struct TLSIntegratedTests { | ||
| let valkeyHostname = ProcessInfo.processInfo.environment["VALKEY_HOSTNAME"] ?? "localhost" | ||
|
|
||
| @available(valkeySwift 1.0, *) | ||
| func withKey<Value>(connection: some ValkeyClientProtocol, _ operation: (ValkeyKey) async throws -> Value) async throws -> Value { | ||
| let key = ValkeyKey(UUID().uuidString) | ||
| let value: Value | ||
| do { | ||
| value = try await operation(key) | ||
| } catch { | ||
| _ = try? await connection.del(keys: [key]) | ||
| throw error | ||
| } | ||
| _ = try await connection.del(keys: [key]) | ||
| return value | ||
| } | ||
|
|
||
| @available(valkeySwift 1.0, *) | ||
| func withValkeyClient( | ||
| _ address: ValkeyServerAddress, | ||
| configuration: ValkeyClientConfiguration = .init(), | ||
| logger: Logger, | ||
| operation: @escaping @Sendable (ValkeyClient) async throws -> Void | ||
| ) async throws { | ||
| try await withThrowingTaskGroup(of: Void.self) { group in | ||
| let client = ValkeyClient(address, configuration: configuration, logger: logger) | ||
| group.addTask { | ||
| await client.run() | ||
| } | ||
| group.addTask { | ||
| try await operation(client) | ||
| } | ||
| try await group.next() | ||
| group.cancelAll() | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| @available(valkeySwift 1.0, *) | ||
| func testSetGet() async throws { | ||
| var logger = Logger(label: "Valkey") | ||
| logger.logLevel = .trace | ||
| let tlsConfiguration = try Self.getTLSConfiguration() | ||
| try await withValkeyClient( | ||
| .hostname(valkeyHostname, port: 6380), | ||
| configuration: .init(tls: .enable(tlsConfiguration, tlsServerName: "Server-only")), | ||
| logger: logger | ||
| ) { connection in | ||
| try await withKey(connection: connection) { key in | ||
| try await connection.set(key, value: "Hello") | ||
| let response = try await connection.get(key).map { String(buffer: $0) } | ||
| #expect(response == "Hello") | ||
| let response2 = try await connection.get("sdf65fsdf").map { String(buffer: $0) } | ||
| #expect(response2 == nil) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static let rootPath = #filePath | ||
| .split(separator: "/", omittingEmptySubsequences: false) | ||
| .dropLast(3) | ||
| .joined(separator: "/") | ||
|
|
||
| @available(valkeySwift 1.0, *) | ||
| static func getTLSConfiguration() throws -> TLSConfiguration { | ||
| do { | ||
| let rootCertificate = try NIOSSLCertificate.fromPEMFile(Self.rootPath + "/valkey/certs/ca.crt") | ||
| let certificate = try NIOSSLCertificate.fromPEMFile(Self.rootPath + "/valkey/certs/client.crt") | ||
| let privateKey = try NIOSSLPrivateKey(file: Self.rootPath + "/valkey/certs/client.key", format: .pem) | ||
| var tlsConfiguration = TLSConfiguration.makeClientConfiguration() | ||
| tlsConfiguration.trustRoots = .certificates(rootCertificate) | ||
| tlsConfiguration.certificateChain = certificate.map { .certificate($0) } | ||
| tlsConfiguration.privateKey = .privateKey(privateKey) | ||
| return tlsConfiguration | ||
| } catch NIOSSLError.failedToLoadCertificate { | ||
| fatalError("Run script ./dev/generate-test-certs.sh to generate test certificates and restart your valkey server.") | ||
| } catch NIOSSLError.failedToLoadPrivateKey { | ||
| fatalError("Run script ./dev/generate-test-certs.sh to generate test certificates and restart your valkey server.") | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private let disableTLSTests: String? = ProcessInfo.processInfo.environment["DISABLE_TLS_TESTS"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #!/bin/bash | ||
| ## | ||
| ## This source file is part of the valkey-swift project | ||
| ## Copyright (c) 2025 the valkey-swift project authors | ||
| ## | ||
| ## See LICENSE.txt for license information | ||
| ## SPDX-License-Identifier: Apache-2.0 | ||
| ## | ||
|
|
||
| # This is a copy of the gen-test-certs.sh script from https://github/valkey-io/valkey | ||
| # BSD 3-Clause License | ||
| # | ||
| # Copyright (c) 2024-present, Valkey contributors | ||
| # All rights reserved. | ||
| # | ||
| # Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: | ||
| # | ||
| # * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. | ||
| # * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. | ||
| # * Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. | ||
| # | ||
| # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| # Generate some test certificates which are used by the regression test suite: | ||
| # | ||
| # valkey/certs/ca.{crt,key} Self signed CA certificate. | ||
| # valkey/certs/client.{crt,key} A certificate restricted for SSL client usage. | ||
| # valkey/certs/server.{crt,key} A certificate restricted for SSL server usage. | ||
| # valkey/certs/valkey.dh DH Params file. | ||
|
|
||
| generate_cert() { | ||
| local name=$1 | ||
| local cn="$2" | ||
| local opts="$3" | ||
|
|
||
| local keyfile=valkey/certs/${name}.key | ||
| local certfile=valkey/certs/${name}.crt | ||
|
|
||
| [ -f "$keyfile" ] || openssl genrsa -out "$keyfile" 2048 | ||
| # shellcheck disable=SC2086 | ||
| openssl req \ | ||
| -new -sha256 \ | ||
| -subj "/O=Valkey Test/CN=$cn" \ | ||
| -key "$keyfile" | \ | ||
| openssl x509 \ | ||
| -req -sha256 \ | ||
| -CA valkey/certs/ca.crt \ | ||
| -CAkey valkey/certs/ca.key \ | ||
| -CAserial valkey/certs/ca.txt \ | ||
| -CAcreateserial \ | ||
| -days 365 \ | ||
| $opts \ | ||
| -out "$certfile" | ||
| # Set all users to read private keys files so CI works | ||
| chmod a+r "$keyfile" | ||
| } | ||
|
|
||
| mkdir -p valkey/certs | ||
| [ -f valkey/certs/ca.key ] || openssl genrsa -out valkey/certs/ca.key 4096 | ||
| openssl req \ | ||
| -x509 -new -nodes -sha256 \ | ||
| -key valkey/certs/ca.key \ | ||
| -days 3650 \ | ||
| -subj '/O=Valkey Test/CN=Certificate Authority' \ | ||
| -out valkey/certs/ca.crt | ||
|
|
||
| cat > valkey/certs/openssl.cnf <<_END_ | ||
| [ server_cert ] | ||
| keyUsage = digitalSignature, keyEncipherment | ||
| nsCertType = server | ||
|
|
||
| [ client_cert ] | ||
| keyUsage = digitalSignature, keyEncipherment | ||
| nsCertType = client | ||
| _END_ | ||
|
|
||
| generate_cert server "Server-only" "-extfile valkey/certs/openssl.cnf -extensions server_cert" | ||
| generate_cert client "Client-only" "-extfile valkey/certs/openssl.cnf -extensions client_cert" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,16 @@ | ||
| # This runs a valkey server with both non-TLS and TLS endpoints. | ||
| # You need to run the script .dev/generate-test-certs.sh to generate the certificates needed for TLS | ||
|
|
||
| services: | ||
| valkey: | ||
| image: valkey/valkey:8.0 | ||
| image: valkey/valkey:9.0 | ||
| ports: | ||
| - 6379:6379 | ||
| healthcheck: | ||
| test: ["CMD", "valkey-cli", "--raw", "incr", "ping"] | ||
| - 6380:6380 | ||
| volumes: | ||
| - ./valkey:/valkey | ||
| - valkey_data:/data | ||
| command: valkey-server --tls-port 6380 --tls-cert-file /valkey/certs/server.crt --tls-key-file /valkey/certs/server.key --tls-ca-cert-file /valkey/certs/ca.crt | ||
|
|
||
| volumes: | ||
| valkey_data: | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this the docker file to be used for local testing ?
And the one in
.devcontaineris used for CI ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
docker-compose.yml is used for testing (non-cluster tests) locally
On macOS you need to run inside a VSCode devcontainer to get cluster working locally. That is what the dev container one is for.