ace-hitl 0.14.0 → 0.16.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.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/.ace-defaults/hitl/config.yml +0 -0
  3. data/.ace-defaults/nav/protocols/skill-sources/ace-hitl.yml +0 -0
  4. data/.ace-defaults/nav/protocols/wfi-sources/ace-hitl.yml +0 -0
  5. data/CHANGELOG.md +20 -0
  6. data/Rakefile +0 -0
  7. data/docs/demo/ace-hitl-scope-and-answer.tape.yml +0 -0
  8. data/docs/demo/fixtures/demo-context.md +0 -0
  9. data/docs/usage.md +47 -2
  10. data/handbook/README.md +0 -0
  11. data/handbook/skills/as-hitl/SKILL.md +0 -0
  12. data/handbook/workflow-instructions/hitl.wf.md +0 -0
  13. data/lib/ace/hitl/atoms/hitl_effect_validator.rb +0 -0
  14. data/lib/ace/hitl/atoms/hitl_file_pattern.rb +0 -0
  15. data/lib/ace/hitl/atoms/hitl_id_formatter.rb +0 -0
  16. data/lib/ace/hitl/cli/commands/create.rb +0 -0
  17. data/lib/ace/hitl/cli/commands/list.rb +0 -0
  18. data/lib/ace/hitl/cli/commands/serve.rb +38 -2
  19. data/lib/ace/hitl/cli/commands/show.rb +0 -0
  20. data/lib/ace/hitl/lifecycle/client.rb +11 -151
  21. data/lib/ace/hitl/lifecycle/credential_path_policy.rb +183 -0
  22. data/lib/ace/hitl/lifecycle/frame_exchange.rb +78 -0
  23. data/lib/ace/hitl/lifecycle/frame_serving.rb +119 -0
  24. data/lib/ace/hitl/lifecycle/lifecycle_requests.rb +101 -0
  25. data/lib/ace/hitl/lifecycle/policy.rb +34 -3
  26. data/lib/ace/hitl/lifecycle/proposals.rb +12 -0
  27. data/lib/ace/hitl/lifecycle/remote_peer.rb +78 -0
  28. data/lib/ace/hitl/lifecycle/service.rb +33 -102
  29. data/lib/ace/hitl/lifecycle/store.rb +1 -0
  30. data/lib/ace/hitl/lifecycle/tls_access_policy.rb +50 -0
  31. data/lib/ace/hitl/lifecycle/tls_authority.rb +121 -0
  32. data/lib/ace/hitl/lifecycle/tls_credentials.rb +210 -0
  33. data/lib/ace/hitl/lifecycle/tls_material.rb +133 -0
  34. data/lib/ace/hitl/lifecycle/tls_project_scope.rb +68 -0
  35. data/lib/ace/hitl/lifecycle/tls_service.rb +242 -0
  36. data/lib/ace/hitl/lifecycle.rb +11 -0
  37. data/lib/ace/hitl/models/hitl_event.rb +0 -0
  38. data/lib/ace/hitl/molecules/hitl_answer_editor.rb +0 -0
  39. data/lib/ace/hitl/molecules/hitl_config_loader.rb +0 -0
  40. data/lib/ace/hitl/molecules/hitl_creator.rb +0 -0
  41. data/lib/ace/hitl/molecules/hitl_display_formatter.rb +0 -0
  42. data/lib/ace/hitl/molecules/hitl_loader.rb +0 -0
  43. data/lib/ace/hitl/molecules/hitl_resolver.rb +0 -0
  44. data/lib/ace/hitl/molecules/hitl_scanner.rb +0 -0
  45. data/lib/ace/hitl/molecules/resume_dispatcher.rb +0 -0
  46. data/lib/ace/hitl/molecules/worktree_scope_resolver.rb +0 -0
  47. data/lib/ace/hitl/proposals/policy.rb +1 -0
  48. data/lib/ace/hitl/providers/lab/assignment_binding.rb +20 -1
  49. data/lib/ace/hitl/version.rb +1 -1
  50. metadata +31 -6
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "securerandom"
4
4
  require "fileutils"
5
+ require "pathname"
5
6
  require "ace/hitl/contract"
6
7
  require_relative "errors"
7
8
  require_relative "identity"
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ace
4
+ module Hitl
5
+ module Lifecycle
6
+ # The authorization seam of one TLS lifecycle authority (spec
7
+ # 8xa.t.nh0): the verified project-scoped client certificate is
8
+ # the ONLY admitted peer and it is bound to the authority's exact
9
+ # project. A certificate for one project cannot authorize another
10
+ # even when issued by the same CA, and no operation against any
11
+ # other project is reachable through this authority.
12
+ class TlsAccessPolicy < AccessPolicy
13
+ def initialize(project:, decision_policy: AccessPolicy.new)
14
+ @project = project.to_s
15
+ @decision_policy = decision_policy
16
+ end
17
+
18
+ def transport?(peer, project: nil)
19
+ bound?(peer) && (project.nil? || project.to_s == @project)
20
+ end
21
+
22
+ def proposal?(peer, project:)
23
+ transport?(peer, project: project)
24
+ end
25
+
26
+ def proposal_silence?(caller_uid:, requester:, project:, operation:)
27
+ return false unless project == @project
28
+ # The transport peer is authenticated separately. A local proposal
29
+ # retains its original kernel principal when Hermes reconciles it.
30
+ return @decision_policy.proposal_silence?(caller_uid: caller_uid, requester: requester,
31
+ project: project, operation: operation) unless caller_uid.nil?
32
+ requester == "urn:ace:hitl:#{@project}:hermes-client" &&
33
+ @decision_policy.silence_operation?(project: project, operation: operation)
34
+ end
35
+
36
+ # A TLS authority has no Unix service uid; client endpoint
37
+ # verification is certificate-based.
38
+ def service_uid
39
+ nil
40
+ end
41
+
42
+ private
43
+
44
+ def bound?(peer)
45
+ peer.is_a?(RemotePeer) && peer.project == @project
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,121 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ipaddr"
4
+ require "json"
5
+ require "ace/runtime/molecules/protected_artifact_set"
6
+ require_relative "tls_credentials"
7
+ require_relative "credential_path_policy"
8
+ require_relative "tls_material"
9
+
10
+ module Ace
11
+ module Hitl
12
+ module Lifecycle
13
+ # The authority configuration of one TLS lifecycle service (spec
14
+ # 8xa.t.nh0): it owns the loopback listen address, the project ID
15
+ # and the server credential reference. The file is a root-owned
16
+ # runtime object (root:<service-group> mode 0640) and every field
17
+ # is closed: unknown fields and nulls are rejected.
18
+ #
19
+ # lab-config owns accounts, file provisioning, the Incus proxy
20
+ # device and the tailnet listener; this configuration only binds
21
+ # the already-provisioned facts.
22
+ class TlsAuthority
23
+ SCHEMA = "ace.hitl.tls.authority/v1"
24
+ FIELDS = %w[schema project_id listen port service_user service_group
25
+ tls_server_credential].freeze
26
+ MAX_BYTES = 65_536
27
+
28
+ attr_reader :project_id, :listen, :port, :service_user, :service_group,
29
+ :credential, :path
30
+
31
+ def self.load(path)
32
+ reader = Ace::Runtime::Molecules::ProtectedArtifactSet.new(file_limit: MAX_BYTES, total_limit: MAX_BYTES)
33
+ reader.with do |held|
34
+ bytes, = held.read_path!(path)
35
+ config = JSON.parse(bytes, create_additions: false, allow_duplicate_key: false, max_nesting: 16)
36
+ authority = new(config, path: path)
37
+ CredentialPathPolicy.verify_runtime_file!(path, service_group: authority.service_group)
38
+ # Exact mode/group validation and parsing must refer to the same
39
+ # bounded protected inode, including every retained ancestor.
40
+ held.verify_unchanged!
41
+ authority
42
+ end
43
+ rescue JSON::ParserError, SystemCallError, IOError, Ace::Runtime::Error
44
+ raise TransportError, "the HITL TLS authority configuration is unavailable or malformed: #{path}"
45
+ end
46
+
47
+ def initialize(config, path: nil)
48
+ unless config.is_a?(Hash) && (config.keys - FIELDS).empty? &&
49
+ FIELDS.all? { |field| !config[field].nil? }
50
+ raise TransportError, "#{SCHEMA} rejects unknown fields and nulls"
51
+ end
52
+ unless config.fetch("schema") == SCHEMA
53
+ raise TransportError, "the HITL TLS authority requires #{SCHEMA}"
54
+ end
55
+
56
+ @path = path
57
+ @project_id = TlsCredentials.project_id(config.fetch("project_id"))
58
+ @listen = validate_listen(config.fetch("listen"))
59
+ @port = validate_port(config.fetch("port"))
60
+ @service_user = config.fetch("service_user")
61
+ @service_group = config.fetch("service_group")
62
+ validate_name(@service_user, "service_user")
63
+ validate_name(@service_group, "service_group")
64
+ @credential = TlsCredentials.parse_server(config.fetch("tls_server_credential"))
65
+ validate_project_binding
66
+ freeze
67
+ end
68
+
69
+ # Fail-closed startup ordering: the exact credential layout is
70
+ # verified BEFORE any TLS material is loaded and BEFORE the
71
+ # listener socket is opened.
72
+ def ssl_context!
73
+ CredentialPathPolicy.verify_credential_tree!(
74
+ credential: @credential, service_user: @service_user, service_group: @service_group)
75
+ TlsMaterial.build_server_context(@credential)
76
+ end
77
+
78
+ private
79
+
80
+ # The container lifecycle service accepts its loopback port
81
+ # only; a wildcard or non-loopback bind would expose the
82
+ # authority beyond the Incus proxy device.
83
+ def validate_listen(value)
84
+ address = IPAddr.new(value.to_s)
85
+ return value.to_s if address.loopback?
86
+
87
+ raise TransportError,
88
+ "the HITL TLS authority must bind a loopback address (got #{value.inspect})"
89
+ rescue IPAddr::InvalidAddressError
90
+ raise TransportError, "the HITL TLS authority listen address is invalid: #{value.inspect}"
91
+ end
92
+
93
+ def validate_port(value)
94
+ unless value.is_a?(Integer) && (1..65_535).cover?(value)
95
+ raise TransportError, "the HITL TLS authority port must be an integer in 1..65535"
96
+ end
97
+
98
+ value
99
+ end
100
+
101
+ def validate_name(value, label)
102
+ return if value.is_a?(String) && value.match?(/\A[a-z_][a-z0-9_-]{0,31}\z/)
103
+
104
+ raise TransportError, "the HITL TLS authority #{label} is not a valid account name"
105
+ end
106
+
107
+ # Both configured identities must name THIS authority's project:
108
+ # a project's proxy reaches only its own lifecycle service.
109
+ def validate_project_binding
110
+ %w[server_uri_san allowed_client_uri_san].each do |field|
111
+ san_project = TlsCredentials.uri_san_project(@credential.fetch(field))
112
+ next if san_project == @project_id
113
+
114
+ raise TransportError,
115
+ "the configured #{field} names project #{san_project.inspect}, not #{@project_id.inspect}"
116
+ end
117
+ end
118
+ end
119
+ end
120
+ end
121
+ end
@@ -0,0 +1,210 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ace
4
+ module Hitl
5
+ module Lifecycle
6
+ # The closed configuration schemas of the selected host/Incus
7
+ # crossing (spec 8xa.t.nh0): configuration contains credential
8
+ # REFERENCES, never secret bytes, and both credential objects
9
+ # reject unknown fields and nulls. The certificate identity is a
10
+ # single URI SAN of the form
11
+ # `urn:ace:hitl:<project-id>:hermes-client` or
12
+ # `urn:ace:hitl:<project-id>:lifecycle-server`; fingerprints are
13
+ # exact lowercase SHA-256 hex digests of the peer leaf certificate.
14
+ #
15
+ # Filesystem layout enforcement lives in CredentialPathPolicy;
16
+ # this module owns only the configuration contract and the
17
+ # certificate-identity helpers shared by the TLS service and the
18
+ # Hermes TLS client.
19
+ module TlsCredentials
20
+ CLIENT_SCHEMA = "ace.hitl.tls.client_credential/v1"
21
+ SERVER_SCHEMA = "ace.hitl.tls.server_credential/v1"
22
+
23
+ CLIENT_FIELDS = %w[certificate_file private_key_file ca_file
24
+ expected_server_uri_san expected_server_certificate_sha256].freeze
25
+ SERVER_FIELDS = %w[certificate_file private_key_file ca_file
26
+ server_uri_san allowed_client_uri_san allowed_client_certificate_sha256].freeze
27
+
28
+ CLIENT_AUTH_EKU = "TLS Web Client Authentication"
29
+ SERVER_AUTH_EKU = "TLS Web Server Authentication"
30
+ CLIENT_AUTH_EKU_OID = "1.3.6.1.5.5.7.3.2"
31
+ SERVER_AUTH_EKU_OID = "1.3.6.1.5.5.7.3.1"
32
+
33
+ # Project ids inside URI SANs exclude ":" so the closed URN form
34
+ # stays unambiguous; every other token rule matches the Hermes
35
+ # token pattern family.
36
+ PROJECT_ID_PATTERN = /\A[A-Za-z0-9][A-Za-z0-9._-]{0,62}\z/
37
+ FINGERPRINT_PATTERN = /\A[0-9a-f]{64}\z/
38
+ URI_SAN_PREFIX = "urn:ace:hitl:"
39
+ ROLES = %w[hermes-client lifecycle-server].freeze
40
+
41
+ # Exactly `tls+tcp://HOST_TAILNET_DNS:PORT`. Literal wildcard,
42
+ # loopback, Unix and cleartext TCP endpoints are invalid; the
43
+ # host is a DNS name (never an IP literal) with at least two
44
+ # labels, and the port is 1..65535.
45
+ REMOTE_ENDPOINT_PATTERN = %r{\Atls\+tcp://((?:[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?\.)+[A-Za-z0-9](?:[A-Za-z0-9-]*[A-Za-z0-9])?):(\d{1,5})\z}
46
+
47
+ Error = Class.new(Lifecycle::Error)
48
+
49
+ module_function
50
+
51
+ # @return [Hash] resolved client credential reference
52
+ def parse_client(config)
53
+ fields = parse_fields(config, CLIENT_FIELDS, CLIENT_SCHEMA)
54
+ validate_uri_san(fields.fetch("expected_server_uri_san"), "lifecycle-server",
55
+ "expected_server_uri_san")
56
+ validate_fingerprint(fields.fetch("expected_server_certificate_sha256"),
57
+ "expected_server_certificate_sha256")
58
+ fields
59
+ end
60
+
61
+ # @return [Hash] resolved server credential reference
62
+ def parse_server(config)
63
+ fields = parse_fields(config, SERVER_FIELDS, SERVER_SCHEMA)
64
+ validate_uri_san(fields.fetch("server_uri_san"), "lifecycle-server", "server_uri_san")
65
+ validate_uri_san(fields.fetch("allowed_client_uri_san"), "hermes-client",
66
+ "allowed_client_uri_san")
67
+ validate_fingerprint(fields.fetch("allowed_client_certificate_sha256"),
68
+ "allowed_client_certificate_sha256")
69
+ fields
70
+ end
71
+
72
+ # @return [Array(String, String, Integer)] dns name, port for a
73
+ # valid remote endpoint string
74
+ def parse_endpoint(value)
75
+ unless value.is_a?(String) && (match = REMOTE_ENDPOINT_PATTERN.match(value))
76
+ raise Error,
77
+ "remote HITL endpoint must be exactly tls+tcp://HOST_TAILNET_DNS:PORT " \
78
+ "(literal wildcard, loopback, IP, Unix and cleartext TCP endpoints are invalid; got #{value.inspect})"
79
+ end
80
+
81
+ port = Integer(match[2])
82
+ raise Error, "remote HITL endpoint port is out of range: #{port}" unless (1..65_535).cover?(port)
83
+
84
+ host = match[1].delete_suffix(".")
85
+ if host.split(".").all? { |label| label.match?(/\A\d+\z/) }
86
+ raise Error,
87
+ "remote HITL endpoint host must be a DNS name; IP literals are invalid (got #{value.inspect})"
88
+ end
89
+
90
+ [host, port]
91
+ end
92
+
93
+ def project_id(value)
94
+ return value if value.is_a?(String) && value.match?(PROJECT_ID_PATTERN)
95
+
96
+ raise Error, "HITL project id must match #{PROJECT_ID_PATTERN.inspect} (got #{value.inspect})"
97
+ end
98
+
99
+ # The project encoded in a closed-form URI SAN.
100
+ # @return [String] project id
101
+ def uri_san_project(uri_san)
102
+ validate_uri_san(uri_san, nil, "uri san")
103
+ uri_san.delete_prefix(URI_SAN_PREFIX).rpartition(":").first
104
+ end
105
+
106
+ def validate_uri_san(value, role, label)
107
+ unless value.is_a?(String) && value.start_with?(URI_SAN_PREFIX)
108
+ raise Error, "#{label} must be a closed-form urn:ace:hitl:<project-id>:<role> URI SAN (got #{value.inspect})"
109
+ end
110
+
111
+ body = value.delete_prefix(URI_SAN_PREFIX)
112
+ project, _, actual_role = body.rpartition(":")
113
+ project_id(project)
114
+ return if role.nil? || actual_role == role
115
+
116
+ raise Error, "#{label} must name the #{role} role (got #{value.inspect})"
117
+ end
118
+
119
+ def validate_fingerprint(value, label)
120
+ return if value.is_a?(String) && value.match?(FINGERPRINT_PATTERN)
121
+
122
+ raise Error, "#{label} must be 64 lowercase hex characters (got #{value.inspect})"
123
+ end
124
+
125
+ # ---- certificate identity helpers (shared by both peers) -----
126
+
127
+ # @return [Array<String>] the URI entries of the subjectAltName
128
+ def uri_sans(certificate)
129
+ san = certificate.extensions.find { |extension| extension.oid == "subjectAltName" }
130
+ return [] unless san
131
+
132
+ san.value.split(/\s*,\s*/).select { |entry| entry.start_with?("URI:") }
133
+ .map { |entry| entry.delete_prefix("URI:") }
134
+ end
135
+
136
+ # @return [Array<String>] rendered extended key usages (short
137
+ # names or dotted OIDs, depending on the OpenSSL build)
138
+ def extended_key_usages(certificate)
139
+ eku = certificate.extensions.find { |extension| extension.oid == "extendedKeyUsage" }
140
+ return [] unless eku
141
+
142
+ eku.value.split(/\s*,\s*/)
143
+ end
144
+
145
+ def client_auth_eku?(certificate)
146
+ usages = extended_key_usages(certificate)
147
+ usages.include?(CLIENT_AUTH_EKU) || usages.include?(CLIENT_AUTH_EKU_OID)
148
+ end
149
+
150
+ def server_auth_eku?(certificate)
151
+ usages = extended_key_usages(certificate)
152
+ usages.include?(SERVER_AUTH_EKU) || usages.include?(SERVER_AUTH_EKU_OID)
153
+ end
154
+
155
+ def sha256_fingerprint(certificate)
156
+ require "digest"
157
+ Digest::SHA256.hexdigest(certificate.to_der)
158
+ end
159
+
160
+ # Exactly one URI SAN, equal to the expected identity. CA
161
+ # membership alone never grants a project: the single SAN is the
162
+ # certificate's whole identity.
163
+ def single_uri_san!(certificate, expected, label)
164
+ sans = uri_sans(certificate)
165
+ unless sans == [expected]
166
+ raise Error,
167
+ "#{label} certificate identity is not the exact single expected URI SAN " \
168
+ "(expected #{expected.inspect}, got #{sans.inspect})"
169
+ end
170
+
171
+ true
172
+ end
173
+
174
+ def valid_time?(certificate, now: Time.now)
175
+ certificate.not_before <= now && now <= certificate.not_after
176
+ end
177
+
178
+ def parse_fields(config, fields, schema)
179
+ unless config.is_a?(Hash)
180
+ raise Error, "#{schema} must be a mapping (got #{config.class})"
181
+ end
182
+
183
+ unknown = config.keys - fields
184
+ unless unknown.empty?
185
+ raise Error, "#{schema} rejects unknown fields: #{unknown.sort.join(", ")}"
186
+ end
187
+
188
+ fields.to_h do |field|
189
+ value = config[field]
190
+ raise Error, "#{schema} field #{field} is missing or null" if value.nil?
191
+ unless value.is_a?(String) && !value.empty?
192
+ raise Error, "#{schema} field #{field} must be a non-empty string"
193
+ end
194
+
195
+ [field, field.end_with?("_file") ? canonical_path(value, schema, field) : value]
196
+ end
197
+ end
198
+
199
+ def canonical_path(value, schema, field)
200
+ unless File.absolute_path?(value) && File.expand_path(value) == value
201
+ raise Error,
202
+ "#{schema} field #{field} must be an absolute canonical path (got #{value.inspect})"
203
+ end
204
+
205
+ value
206
+ end
207
+ end
208
+ end
209
+ end
210
+ end
@@ -0,0 +1,133 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "openssl"
4
+
5
+ module Ace
6
+ module Hitl
7
+ module Lifecycle
8
+ # Loads the certificate material referenced by a resolved TLS
9
+ # credential and builds the mutual-TLS 1.3 SSL contexts of the
10
+ # host/Incus crossing (spec 8xa.t.nh0).
11
+ #
12
+ # This module owns PEM parsing and context assembly only; the
13
+ # deployment layout (ownership/modes/symlinks) is verified by
14
+ # CredentialPathPolicy at the production entry points, and the
15
+ # PEER identity (SAN/fingerprint/EKU/DNS/CA) is verified after
16
+ # each handshake by the TLS service and the Hermes TLS client.
17
+ module TlsMaterial
18
+ Error = Class.new(Lifecycle::Error)
19
+
20
+ class << self
21
+ # @return [OpenSSL::SSL::SSLContext] server context requiring
22
+ # a client certificate issued by the configured CA
23
+ def build_server_context(credential)
24
+ leaf, chain, key = load_identity(credential)
25
+ verify_server_identity!(leaf, credential, key)
26
+
27
+ build_context(credential) do |ctx|
28
+ ctx.cert = leaf
29
+ ctx.extra_chain_cert = chain unless chain.empty?
30
+ ctx.key = key
31
+ ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT
32
+ ctx.verify_hostname = false
33
+ end
34
+ end
35
+
36
+ # @return [OpenSSL::SSL::SSLContext] client context presenting
37
+ # the project-scoped client identity; DNS verification uses
38
+ # the hostname set on the SSLSocket before the handshake
39
+ def build_client_context(credential)
40
+ leaf, chain, key = load_identity(credential)
41
+ verify_client_identity!(leaf)
42
+
43
+ build_context(credential) do |ctx|
44
+ ctx.cert = leaf
45
+ ctx.extra_chain_cert = chain unless chain.empty?
46
+ ctx.key = key
47
+ ctx.verify_mode = OpenSSL::SSL::VERIFY_PEER
48
+ ctx.verify_hostname = true
49
+ end
50
+ end
51
+
52
+ def verify_server_identity!(leaf, credential, key)
53
+ unless TlsCredentials.server_auth_eku?(leaf)
54
+ raise Error, "the lifecycle server certificate lacks the serverAuth EKU"
55
+ end
56
+ TlsCredentials.single_uri_san!(leaf, credential.fetch("server_uri_san"),
57
+ "lifecycle server")
58
+ unless TlsCredentials.valid_time?(leaf)
59
+ raise Error, "the lifecycle server certificate is outside its validity window"
60
+ end
61
+ unless leaf.public_key.compare?(key.public_key)
62
+ raise Error, "the lifecycle server private key does not match its certificate"
63
+ end
64
+ end
65
+
66
+ def verify_client_identity!(leaf)
67
+ unless TlsCredentials.client_auth_eku?(leaf)
68
+ raise Error, "the Hermes client certificate lacks the clientAuth EKU"
69
+ end
70
+ unless TlsCredentials.valid_time?(leaf)
71
+ raise Error, "the Hermes client certificate is outside its validity window"
72
+ end
73
+ end
74
+
75
+ # @return [Array(OpenSSL::X509::Certificate, Array, OpenSSL::PKey)]
76
+ # leaf certificate, remaining chain, private key
77
+ def load_identity(credential)
78
+ certificates = load_certificates(credential.fetch("certificate_file"))
79
+ key = load_key(credential.fetch("private_key_file"))
80
+ [certificates.first, certificates.drop(1), key]
81
+ end
82
+
83
+ def load_certificates(path)
84
+ certificates = scan_pem(path, "CERTIFICATE").map do |pem|
85
+ OpenSSL::X509::Certificate.new(pem)
86
+ end
87
+ raise Error, "the certificate file contains no certificate: #{path}" if certificates.empty?
88
+
89
+ certificates
90
+ end
91
+
92
+ def load_key(path)
93
+ pem = read_file(path)
94
+ OpenSSL::PKey.read(pem)
95
+ rescue OpenSSL::PKey::PKeyError
96
+ raise Error, "the private key file is not a readable private key: #{path}"
97
+ end
98
+
99
+ private
100
+
101
+ def build_context(credential)
102
+ ctx = OpenSSL::SSL::SSLContext.new
103
+ ctx.set_params
104
+ ctx.min_version = OpenSSL::SSL::TLS1_3_VERSION
105
+ ctx.max_version = OpenSSL::SSL::TLS1_3_VERSION
106
+ # An explicit per-context trust store: the configured issuing
107
+ # CA is the ONLY trust anchor of this authority or client,
108
+ # never an accumulated or shared default store (identical
109
+ # subject collisions in shared stores would poison issuer
110
+ # selection across peers in one process).
111
+ store = OpenSSL::X509::Store.new
112
+ store.add_file(credential.fetch("ca_file"))
113
+ ctx.cert_store = store
114
+ yield ctx
115
+ ctx
116
+ rescue OpenSSL::SSL::SSLError => e
117
+ raise Error, "the TLS context of the HITL crossing is not usable (#{e.message})"
118
+ end
119
+
120
+ def scan_pem(path, label)
121
+ read_file(path).scan(%r{-----BEGIN #{label}-----.*?-----END #{label}-----}m)
122
+ end
123
+
124
+ def read_file(path)
125
+ File.read(path)
126
+ rescue SystemCallError, IOError
127
+ raise Error, "the TLS credential file is not readable: #{path}"
128
+ end
129
+ end
130
+ end
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "delegate"
4
+
5
+ module Ace
6
+ module Hitl
7
+ module Lifecycle
8
+ # The one-project guard of a TLS lifecycle authority (spec
9
+ # 8xa.t.nh0): the verified remote peer is bound to EXACTLY ONE
10
+ # project, so every flow that carries or could resolve to another
11
+ # project is refused BEFORE store access — pending flows carry the
12
+ # channel's project, the global states projection and foreign
13
+ # creates are unreachable, and cross-authority correlation is
14
+ # impossible through this boundary.
15
+ class TlsProjectScope < SimpleDelegator
16
+ def initialize(store, project:)
17
+ super(store)
18
+ @project = project
19
+ end
20
+
21
+ def pending(project: nil, after: nil)
22
+ require_bound_project!(project)
23
+ super(project: @project, after: after)
24
+ end
25
+
26
+ def pending_page(project: nil, after: nil)
27
+ require_bound_project!(project)
28
+ super(project: @project, after: after)
29
+ end
30
+
31
+ # The global cross-project projection is not reachable through a
32
+ # single-project authority.
33
+ def states
34
+ raise PermissionError, "the TLS authority serves only project #{@project.inspect}"
35
+ end
36
+
37
+ def create(**params)
38
+ require_bound_project!(params[:project])
39
+ super
40
+ end
41
+
42
+ def proposal_wake(project:, after: nil)
43
+ require_bound_project!(project)
44
+ super(project: @project, after: after)
45
+ end
46
+
47
+ def proposal_due(project:, after: nil)
48
+ require_bound_project!(project)
49
+ super(project: @project, after: after)
50
+ end
51
+
52
+ def proposal_history(project:, query: "", after: nil)
53
+ require_bound_project!(project)
54
+ super(project: @project, query: query, after: after)
55
+ end
56
+
57
+ private
58
+
59
+ def require_bound_project!(project)
60
+ return if project.nil? || project.to_s == @project
61
+
62
+ raise PermissionError,
63
+ "the TLS authority of project #{@project.inspect} never serves project #{project.to_s.inspect}"
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end