Skip to content
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

Couldn't create secret object #2025

Closed
ddowding opened this issue May 9, 2018 · 4 comments · Fixed by #2793
Closed

Couldn't create secret object #2025

ddowding opened this issue May 9, 2018 · 4 comments · Fixed by #2793

Comments

@ddowding
Copy link

ddowding commented May 9, 2018

I couldn't create secret object, the problem seemed to boil down to the way that a secret was being created from the docker daemon response.

def create(self, **kwargs):
obj = self.client.api.create_secret(**kwargs)
return self.prepare_model(obj)

Docker version 18.03.1-ce and python version 3.5 had the following error:

File "docker/models/secrets.py", line 10 in __repr__
return "<%s: %s'>" % (self.__class__.__name__, self.name)
File "docker/models/secrets.py", line 14 in name
return self.attrs['Spec']['Name']
KeyError: 'Spec'

When calling:

import docker

client -docker.from_env()
mySecret = client.secrets.create(name='randomName', data='platform_node_requirements.md')

Changing the code to the following seemed to fix it.

obj = self.client.api.create_secret(**kwargs)
secret = self.client.secrets.get(obj.get('ID'))
return self.prepare_model(secret)
@douglasmiranda
Copy link

Master branch is working.

@remorses
Copy link

I get the same error for client.configs.create

@etienne-napoleone
Copy link

etienne-napoleone commented Nov 28, 2019

docker version: 19.03.5 633a0ea
docker-py version: 4.1.0

On my side the creation works but the __repr__ method seem to expect a name where the returned object only contains the id attr. Not too bad but it could be handled more gracefully. For example you will get an ugly exception every time you create a secret from an interactive python shell without assigning the returned value.

>>> import docker
>>> d = docker.from_env()

>>> s = d.secrets.create(name="test", data="ok")
>>> s
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/Library/Caches/pypoetry/virtualenvs/whatever-py3.7/lib/python3.7/site-packages/docker/models/secrets.py", line 10, in __repr__
    return "<%s: '%s'>" % (self.__class__.__name__, self.name)
  File "/Users/user/Library/Caches/pypoetry/virtualenvs/whatever-py3.7/lib/python3.7/site-packages/docker/models/secrets.py", line 14, in name
    return self.attrs['Spec']['Name']
KeyError: 'Spec'
>>> s.attrs
{'ID': 'wi2jpy7n70wkkrbytnbctv1nj'}

>>> d.secrets.create(name="test2", data="ok")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/user/Library/Caches/pypoetry/virtualenvs/whatever-py3.7/lib/python3.7/site-packages/docker/models/secrets.py", line 10, in __repr__
    return "<%s: '%s'>" % (self.__class__.__name__, self.name)
  File "/Users/user/Library/Caches/pypoetry/virtualenvs/whatever-py3.7/lib/python3.7/site-packages/docker/models/secrets.py", line 14, in name
    return self.attrs['Spec']['Name']
KeyError: 'Spec'

feliperuhland added a commit to feliperuhland/docker-py that referenced this issue Nov 28, 2019
- Change id_attribute to Id
- add new property id
- add name fallback

Fixes docker#2025
feliperuhland added a commit to feliperuhland/docker-py that referenced this issue Nov 28, 2019
- Change id_attribute to Id
- add new property id
- add name fallback

Fixes docker#2025

Signed-off-by: Felipe Ruhland <felipe.ruhland@gmail.com>
@feliperuhland
Copy link
Contributor

Hi @etienne-napoleone

I opened a PR for that issue. Do you want to review it?
Thanks.

feliperuhland added a commit to feliperuhland/docker-py that referenced this issue Nov 28, 2019
- Change id_attribute to Id
- add new property id
- add name fallback

Fixes docker#2025

Signed-off-by: Felipe Ruhland <felipe.ruhland@gmail.com>
feliperuhland added a commit to feliperuhland/docker-py that referenced this issue Feb 19, 2020
- Change id_attribute to Id
- add new property id
- add name fallback

Fixes docker#2025

Signed-off-by: Felipe Ruhland <felipe.ruhland@gmail.com>
feliperuhland added a commit to feliperuhland/docker-py that referenced this issue Feb 25, 2021
- Change id_attribute to Id
- add new property id
- add name fallback

Fixes docker#2025

Signed-off-by: Felipe Ruhland <felipe.ruhland@gmail.com>
feliperuhland added a commit to feliperuhland/docker-py that referenced this issue Mar 24, 2021
How to reproduce the issue:

```py
>>> import docker
>>> cli = docker.from_env()
>>> cli.secrets.create(name="any_name", data="1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ruhland/projects/github.com/feliperuhland/docker-py/docker/models/secrets.py", line 10, in __repr__
    return "<%s: '%s'>" % (self.__class__.__name__, self.name)
  File "/home/ruhland/projects/github.com/feliperuhland/docker-py/docker/models/secrets.py", line 14, in name
    return self.attrs['Spec']['Name']
KeyError: 'Spec'
```

The exception raises because create secrets API `/secrets/create` only
return the `id` attribute:
https://docs.docker.com/engine/api/v1.41/#operation/SecretCreate
The secret model is created using just the `id` attribute and fails
when looking for Spec.Name attribute.

```py
def __repr__(self):
    return "<%s: '%s'>" % (self.__class__.__name__, self.name)
```

```py
@Property
def name(self):
    return self.attrs['Spec']['Name']
```

I came up with a ugly solution but will prevent the problem to happen
again:

```py
def create(self, **kwargs):
    obj = self.client.api.create_secret(**kwargs)
+   obj.setdefault("Spec", {})["Name"] = kwargs.get("name")
    return self.prepare_model(obj)
```

After the API call, I added the name attribute to the right place to be
used on the property name.

```py
>>> import docker
>>> cli = docker.from_env()
>>> cli.secrets.create(name="any_name", data="1")
<Secret: 'any_name'>
```

It isn't the most elegant solution, but it will do the trick.
I had a previous PR docker#2517 when I propose using the `id` attribute
instead of `name` on the `__repr__` method, but I think this one will be better.

That fixes docker#2025
feliperuhland added a commit to feliperuhland/docker-py that referenced this issue Mar 24, 2021
How to reproduce the issue:

```py
>>> import docker
>>> cli = docker.from_env()
>>> cli.secrets.create(name="any_name", data="1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ruhland/projects/github.com/feliperuhland/docker-py/docker/models/secrets.py", line 10, in __repr__
    return "<%s: '%s'>" % (self.__class__.__name__, self.name)
  File "/home/ruhland/projects/github.com/feliperuhland/docker-py/docker/models/secrets.py", line 14, in name
    return self.attrs['Spec']['Name']
KeyError: 'Spec'
```

The exception raises because create secrets API `/secrets/create` only
return the `id` attribute:
https://docs.docker.com/engine/api/v1.41/#operation/SecretCreate
The secret model is created using just the `id` attribute and fails
when looking for Spec.Name attribute.

```py
def __repr__(self):
    return "<%s: '%s'>" % (self.__class__.__name__, self.name)
```

```py
@Property
def name(self):
    return self.attrs['Spec']['Name']
```

I came up with a ugly solution but will prevent the problem to happen
again:

```py
def create(self, **kwargs):
    obj = self.client.api.create_secret(**kwargs)
+   obj.setdefault("Spec", {})["Name"] = kwargs.get("name")
    return self.prepare_model(obj)
```

After the API call, I added the name attribute to the right place to be
used on the property name.

```py
>>> import docker
>>> cli = docker.from_env()
>>> cli.secrets.create(name="any_name", data="1")
<Secret: 'any_name'>
```

It isn't the most elegant solution, but it will do the trick.
I had a previous PR docker#2517 when I propose using the `id` attribute
instead of `name` on the `__repr__` method, but I think this one will be better.

That fixes docker#2025

Signed-off-by: Felipe Ruhland <felipe.ruhland@gmail.com>
feliperuhland added a commit to feliperuhland/docker-py that referenced this issue Mar 24, 2021
How to reproduce the issue:

```py
>>> import docker
>>> cli = docker.from_env()
>>> cli.secrets.create(name="any_name", data="1")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/docker-py/docker/models/secrets.py", line 10, in __repr__
    return "<%s: '%s'>" % (self.__class__.__name__, self.name)
  File "/home/docker-py/docker/models/secrets.py", line 14, in name
    return self.attrs['Spec']['Name']
KeyError: 'Spec'
```

The exception raises because create secrets API `/secrets/create` only
return the `id` attribute:
https://docs.docker.com/engine/api/v1.41/#operation/SecretCreate
The secret model is created using just the `id` attribute and fails
when looking for Spec.Name attribute.

```py
def __repr__(self):
    return "<%s: '%s'>" % (self.__class__.__name__, self.name)
```

```py
@Property
def name(self):
    return self.attrs['Spec']['Name']
```

I came up with a ugly solution but will prevent the problem to happen
again:

```py
def create(self, **kwargs):
    obj = self.client.api.create_secret(**kwargs)
+   obj.setdefault("Spec", {})["Name"] = kwargs.get("name")
    return self.prepare_model(obj)
```

After the API call, I added the name attribute to the right place to be
used on the property name.

```py
>>> import docker
>>> cli = docker.from_env()
>>> cli.secrets.create(name="any_name", data="1")
<Secret: 'any_name'>
```

It isn't the most elegant solution, but it will do the trick.
I had a previous PR docker#2517 when I propose using the `id` attribute
instead of `name` on the `__repr__` method, but I think this one will be better.

That fixes docker#2025

Signed-off-by: Felipe Ruhland <felipe.ruhland@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants