Fabric: AttributeError: 'str' object has no attribute 'extend' when using custom connect_kwargs and key_filename option

Created on 31 Aug 2019  ·  6Comments  ·  Source: fabric/fabric

When using a custom connect_kwargs with a key_filename option, like so:

    config = fabric.config.Config()
    print("Connecting with ssh/config")
    with fabric.Connection(
        host=instancePrivateIp,
        user='ec2-user',
        config=config,
        connect_kwargs={
            "key_filename": keyFileName,
        }) as c_instance:

I get an AttributeError:

  File "/home/dumontj/Projects/ami-builder/ami_builder/customization.py", line 26, in do_bash_customization
    "key_filename": keyFileName,
  File "/home/dumontj/.local/share/virtualenvs/ami-builder-jbdgQc96/lib/python3.7/site-packages/fabric/connection.py", line 450, in __init__
    self.connect_kwargs = self.resolve_connect_kwargs(connect_kwargs)
  File "/home/dumontj/.local/share/virtualenvs/ami-builder-jbdgQc96/lib/python3.7/site-packages/fabric/connection.py", line 490, in resolve_connect_kwargs
    connect_kwargs["key_filename"].extend(
AttributeError: 'str' object has no attribute 'extend'

The documentation specifies that key_filename should be given as a string, but I found that it works if you actually give it a list:

    with fabric.Connection(
        host=instancePrivateIp,
        user='ec2-user',
        config=config,
        connect_kwargs={
            "key_filename": [keyFileName,],
        }) as c_instance:

If this is the wanted syntax, I can update the documentation through a PR (see https://github.com/fabric/fabric/blob/2.0/fabric/connection.py#L234). Otherwise I could change fabric/connection.py to check whether there is already an entry, then transform it into a list if necessary.

Most helpful comment

I'm seeing the same issue on 2.5. However, using a list works.

All 6 comments

I'm seeing the same issue on 2.5. However, using a list works.

Same problem. Looks like it could be a silly bug to me, so pinning to 2.4.0 till we hear more from upstream.

I get the same problem when host is set to a DNS name. When set to an IP, the exception doesn't occur.
E.g.:

{'host': '127.0.0.1', 'user': 'MyUser', 'connect_kwargs': {'key_filename': '/Users/MyUser/.ssh/MyPem.pem'}} # <- Works
{'host': 'some.dns.name', 'user': 'MyUser', 'connect_kwargs': {'key_filename': '/Users/MyUser/.ssh/MyPem.pem'}} # <- Doesn't work

face_caches[self.face_img_paths[i].stem] = faces
AttributeError: 'str' object has no attribute 'stem'
i got this error. can anyone suggest me a solution

Someone else made a PR to change the docs in Sep 2019, but not merged yet (Feb 2020): #2014

This happens to me as well... and also, I think it would be important to be able to set if I only want to connect using the key_filename provided, instead of using the ssh_config info.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jmcgrath207 picture jmcgrath207  ·  5Comments

supriyopaul picture supriyopaul  ·  4Comments

TimotheeJeannin picture TimotheeJeannin  ·  3Comments

peteruhnak picture peteruhnak  ·  4Comments

bitprophet picture bitprophet  ·  4Comments