connect()¶
Open and return a ready Connection (blocking).
Four transport paths
- message server:
mshostset → resolve the least-loaded app server via MessageServerClient.resolve(group), then direct-TCP connect (TRANS-03). - SAProuter:
saprouterset → prepend the NI_ROUTE prefix before the direct-TCP handshake (TRANS-02). - wRFC (WebSocket RFC over TLS):
wshostset → route throughconnect_ws(SEC-05, D-16/D-17).wsportdefaults to 443 andws_pathto/sap/bc/rfc(D-19). Optionalws_proxy_*params tunnel the connection through an HTTP CONNECT forward proxy (D-20). - SNC (Secure Network Communications):
snc_libset (andwshostabsent) → wrap the direct-TCP transport in an :class:~saprfclib.snc.SncTransportthat drives the GSS-API handshake to COMPLETE before any data is sent (SEC-02/03/04/06, D-13).snc_libpresence is the activation switch — there is no separate mode flag.snc_ssodefaults to False (D-12).wshosttakes precedence: SNC-over-wRFC is out of scope for Phase 7.
snc_qop selects the protection level and defaults to 3. Accepted
values are 1 (authentication only), 2 (integrity), 3 (privacy),
8 (the system default) and 9 (the maximum available); anything else
raises ValueError rather than picking a level, because the value
decides whether payloads are encrypted.
8 is an approximation. SAP defines it as "apply the system's default
protection", which comes from the server's snc/data_protection/use
profile parameter. This library does not read that parameter, so 8 is
treated exactly as 3. That errs toward more protection rather than less,
which is the only direction it is safe to approximate in — but it is an
approximation, not a negotiated answer. Pass 1, 2 or 3 if you need to know
precisely what is applied.
- direct: port = 3300 + int(sysnr) (gateway port unless port is
given), connect_tcp, handshake.
proxy_type="socks5" routes the raw-TCP connection through a SOCKS5 proxy —
the interface the SAP BTP Connectivity Proxy exposes so Kyma/Kubernetes
workloads reach on-premise systems via SAP Cloud Connector (issue #51, D-39).
proxy_host and proxy_port are required (no default is assumed). SAP
uses two SOCKS5 authentication methods and never RFC 1929 user/password (D-40):
- no authentication (method 0x00) when none of the JWT parameters are
set — the Connectivity Proxy in trusted mode
(
enableProxyAuthorization=false, the default for in-cluster workloads). - SAP JWT (method 0x80) when
proxy_jwtis supplied, or whenproxy_client_id/proxy_client_secret/proxy_token_urlare all set — then aclient_credentialstoken is fetched (D-41). An optionalproxy_scc_location_idselects a specific Cloud Connector location.
socket_factory is the generic form of the same seam: a
Callable[[host, port], socket] that returns an already-connected socket. It
is mutually exclusive with proxy_type. Neither applies to the wRFC
(wshost) path, which proxies via ws_proxy_*.
gwhost is the gateway host written into the GW handshake frames, which the
server-side gateway resolves. It defaults to ashost and should be left
unset for a direct connection. Set it for a proxy / Cloud Connector path:
ashost is then the virtual host used only to open the TCP tunnel (resolved
by the proxy), while gwhost must be the SAP system's own internal hostname —
the name the gateway resolves to itself. A virtual gwhost makes the gateway
reject the connection with hostname '…' unknown (issue #51).
lang is the logon language. Accepts the one-character SAP code ('E' English,
'D' German, 'S' Spanish, …) or the two-character ISO code ('EN', 'DE', 'ES'); an
ISO code is converted before the logon frame is built, matching the SDK's LANG
option.
user and passwd may both be omitted. That is read as a deliberate
anonymous attempt and the logon frame goes out without the user and password
records — some systems answer a small set of function modules that way, while a
hardened one refuses below the RFC layer and raises CommunicationError.
Supplying exactly one of the two raises ValueError, since that is a missing
setting rather than a request to connect anonymously. SNC connections are
unaffected: snc_lib carries its own credentials.
strict_params controls what call() does with a keyword argument the
function interface does not declare. The default (False) drops it and logs a
warning, which is what callers porting from pyrfc expect when they pass a
superset of kwargs across differing SAP releases. Set True to raise ValueError
instead — worth doing when a dropped argument would change the result, since the
server has no way to tell you an argument never arrived.
allow_restricted_logon (default False) governs a logon that AUTHENTICATED
but whose embedded RFCPING the server rejected — typically S_RFC
(RFC_NO_AUTHORITY) for a low-privilege user (e.g. TMSADM). By default
that raises :class:~saprfclib.exceptions.AbapApplicationError with the
server's exception, matching the reference client. Set True to return a usable
connection instead: authentication and function authorization are separate, and
the session is ready for the caller's own authorized calls. Intended for tools
that enumerate restricted RFC destinations (issue #38).
trace attaches an :class:~saprfclib.trace.RfcTrace, which writes an
SDK-format trace file of every frame. It is a parameter rather than an
environment variable on purpose: the SDK reads RFC_TRACE from the
environment, but a process that starts writing traffic to disk because of a
variable it inherited is a surprise, and the file — though credential-redacted
— still contains everything else that crossed the wire. Turning it on should
be visible at the call site.
The SAProuter and message-server wire formats were live-verified after this
docstring first called them unverified: the NI_ROUTE payload is byte-exact
against a capture (tests/golden/router/ni_route_payload.bin), a router
that accepts a route answers NI_PONG and one that refuses answers
NI_RTERR, and the message server answers the binary attach and
server-list frames as MSG_SERVER. What remains unconfirmed is narrower and
sits in router.py: some field boundaries inside a server-list entry, and
whether the entry count is carried in the header or only implied by the
payload length. passwd,
ws_proxy_pass, proxy_jwt, proxy_client_secret, snc_lib,
snc_partnername and snc_myname are never logged or echoed into any log
message or exception string (threats T-03-CRED2 / T-07-CRED / T-07-PROXY-CRED).
Source code in src/saprfclib/connection.py
4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 | |