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

Fix secret repr #2517

Closed
wants to merge 4 commits into from
Closed

Conversation

feliperuhland
Copy link
Contributor

This PR fixes issue #2025

@feliperuhland
Copy link
Contributor Author

@shin- @ulyssessouza

Could you take a look?

Thanks.

Copy link
Contributor

@ulyssessouza ulyssessouza left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some considerations on why the replacement of name by id

docker/models/secrets.py Outdated Show resolved Hide resolved

def __repr__(self):
return "<%s: '%s'>" % (self.__class__.__name__, self.name)
return "<%s: '%s'>" % (self.__class__.__name__, self.id)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why changing the attribute here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's because List Secrets doesn't return the name attribute.

Issue #2025

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well...
The API says it does. That's in the very link you mentioned. If not, it's a bug

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name is nested in the Spec in the response, so not at the same level as id;

[
  {
    "ID": "ktnbjxoalbkvbvedmg1urrz8h",
    "Version": {
      "Index": 11
    },
    "CreatedAt": "2016-11-05T01:20:17.327670065Z",
    "UpdatedAt": "2016-11-05T01:20:17.327670065Z",
    "Spec": {
      "Name": "app-dev.crt"
    }
  }
]

Screenshot 2020-02-28 at 17 01 01

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @ulyssessouza @thaJeztah the secret creation endpoint just return the ID value, so the method __repr__ raise an exception:

     12     @property
     13     def name(self):
---> 14         return self.attrs['Spec']['Name']
     15 
     16     def remove(self):

KeyError: 'Spec'

image

@GordonTheTurtle
Copy link

Please sign your commits following these rules:
https://github.com/moby/moby/blob/master/CONTRIBUTING.md#sign-your-work
The easiest way to do this is to amend the last commit:

$ git clone -b "fix-secret-repr" git@github.com:feliperuhland/docker-py.git somewhere
$ cd somewhere
$ git rebase -i HEAD~842354382592
editor opens
change each 'pick' to 'edit'
save the file and quit
$ git commit --amend -s --no-edit
$ git rebase --continue # and repeat the amend for each commit
$ git push -f

Amending updates the existing PR. You DO NOT need to open a new one.

- a post fake secret
- added to fake api
- create a unit test

Signed-off-by: Felipe Ruhland <felipe.ruhland@gmail.com>
- Change id_attribute to Id
- add new property id
- add name fallback

Fixes docker#2025

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

Hi @ulyssessouza @thaJeztah @shin-

Could you please take a look?
Thanks!

@feliperuhland
Copy link
Contributor Author

Hi @ulyssessouza @thaJeztah @shin- @aiordache
Could you please take a look?

Thanks!

feliperuhland added a commit to feliperuhland/docker-py that referenced this pull request 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 pull request 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 pull request 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 this pull request may close these issues.

4 participants