Skip to content

Commit

Permalink
Merge pull request #9673 from certbot/types-dns-common-get
Browse files Browse the repository at this point in the history
types: CredentialsConfiguration.conf can return None
  • Loading branch information
wgreenberg committed Apr 18, 2023
2 parents 31094bc + b9ec315 commit 399b932
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import Any
from typing import Callable
from typing import cast
from typing import Optional

import digitalocean
Expand Down Expand Up @@ -56,7 +57,7 @@ def _cleanup(self, domain: str, validation_name: str, validation: str) -> None:
def _get_digitalocean_client(self) -> "_DigitalOceanClient":
if not self.credentials: # pragma: no cover
raise errors.Error("Plugin has not been prepared.")
return _DigitalOceanClient(self.credentials.conf('token'))
return _DigitalOceanClient(cast(str, self.credentials.conf('token')))


class _DigitalOceanClient:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import Any
from typing import Callable
from typing import cast
from typing import Optional

from lexicon.providers import dnsimple
Expand Down Expand Up @@ -58,7 +59,7 @@ def _cleanup(self, domain: str, validation_name: str, validation: str) -> None:
def _get_dnsimple_client(self) -> "_DNSimpleLexiconClient":
if not self.credentials: # pragma: no cover
raise errors.Error("Plugin has not been prepared.")
return _DNSimpleLexiconClient(self.credentials.conf('token'), self.ttl)
return _DNSimpleLexiconClient(cast(str, self.credentials.conf('token')), self.ttl)


class _DNSimpleLexiconClient(dns_common_lexicon.LexiconClient):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import Any
from typing import Callable
from typing import cast
from typing import Optional

from lexicon.providers import dnsmadeeasy
Expand Down Expand Up @@ -62,8 +63,8 @@ def _cleanup(self, domain: str, validation_name: str, validation: str) -> None:
def _get_dnsmadeeasy_client(self) -> "_DNSMadeEasyLexiconClient":
if not self.credentials: # pragma: no cover
raise errors.Error("Plugin has not been prepared.")
return _DNSMadeEasyLexiconClient(self.credentials.conf('api-key'),
self.credentials.conf('secret-key'),
return _DNSMadeEasyLexiconClient(cast(str, self.credentials.conf('api-key')),
cast(str, self.credentials.conf('secret-key')),
self.ttl)


Expand Down
5 changes: 3 additions & 2 deletions certbot-dns-gehirn/certbot_dns_gehirn/_internal/dns_gehirn.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import Any
from typing import Callable
from typing import cast
from typing import Optional

from lexicon.providers import gehirn
Expand Down Expand Up @@ -64,8 +65,8 @@ def _get_gehirn_client(self) -> "_GehirnLexiconClient":
if not self.credentials: # pragma: no cover
raise errors.Error("Plugin has not been prepared.")
return _GehirnLexiconClient(
self.credentials.conf('api-token'),
self.credentials.conf('api-secret'),
cast(str, self.credentials.conf('api-token')),
cast(str, self.credentials.conf('api-secret')),
self.ttl
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
from typing import Any
from typing import Callable
from typing import cast
from typing import Optional
from typing import Union

Expand Down Expand Up @@ -61,7 +62,7 @@ def _cleanup(self, domain: str, validation_name: str, validation: str) -> None:
def _get_linode_client(self) -> '_LinodeLexiconClient':
if not self.credentials: # pragma: no cover
raise errors.Error("Plugin has not been prepared.")
api_key = self.credentials.conf('key')
api_key = cast(str, self.credentials.conf('key'))
api_version: Optional[Union[str, int]] = self.credentials.conf('version')
if api_version == '':
api_version = None
Expand Down
5 changes: 3 additions & 2 deletions certbot-dns-luadns/certbot_dns_luadns/_internal/dns_luadns.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import Any
from typing import Callable
from typing import cast
from typing import Optional

from lexicon.providers import luadns
Expand Down Expand Up @@ -59,8 +60,8 @@ def _cleanup(self, domain: str, validation_name: str, validation: str) -> None:
def _get_luadns_client(self) -> "_LuaDNSLexiconClient":
if not self.credentials: # pragma: no cover
raise errors.Error("Plugin has not been prepared.")
return _LuaDNSLexiconClient(self.credentials.conf('email'),
self.credentials.conf('token'),
return _LuaDNSLexiconClient(cast(str, self.credentials.conf('email')),
cast(str, self.credentials.conf('token')),
self.ttl)


Expand Down
3 changes: 2 additions & 1 deletion certbot-dns-nsone/certbot_dns_nsone/_internal/dns_nsone.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import Any
from typing import Callable
from typing import cast
from typing import Optional

from lexicon.providers import nsone
Expand Down Expand Up @@ -58,7 +59,7 @@ def _cleanup(self, domain: str, validation_name: str, validation: str) -> None:
def _get_nsone_client(self) -> "_NS1LexiconClient":
if not self.credentials: # pragma: no cover
raise errors.Error("Plugin has not been prepared.")
return _NS1LexiconClient(self.credentials.conf('api-key'), self.ttl)
return _NS1LexiconClient(cast(str, self.credentials.conf('api-key')), self.ttl)


class _NS1LexiconClient(dns_common_lexicon.LexiconClient):
Expand Down
9 changes: 5 additions & 4 deletions certbot-dns-ovh/certbot_dns_ovh/_internal/dns_ovh.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import Any
from typing import Callable
from typing import cast
from typing import Optional

from lexicon.providers import ovh
Expand Down Expand Up @@ -65,10 +66,10 @@ def _get_ovh_client(self) -> "_OVHLexiconClient":
if not self.credentials: # pragma: no cover
raise errors.Error("Plugin has not been prepared.")
return _OVHLexiconClient(
self.credentials.conf('endpoint'),
self.credentials.conf('application-key'),
self.credentials.conf('application-secret'),
self.credentials.conf('consumer-key'),
cast(str, self.credentials.conf('endpoint')),
cast(str, self.credentials.conf('application-key')),
cast(str, self.credentials.conf('application-secret')),
cast(str, self.credentials.conf('consumer-key')),
self.ttl
)

Expand Down
13 changes: 7 additions & 6 deletions certbot-dns-rfc2136/certbot_dns_rfc2136/_internal/dns_rfc2136.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import Any
from typing import Callable
from typing import cast
from typing import Optional

import dns.flags
Expand Down Expand Up @@ -59,7 +60,7 @@ def more_info(self) -> str:
'RFC 2136 Dynamic Updates.'

def _validate_credentials(self, credentials: CredentialsConfiguration) -> None:
server = credentials.conf('server')
server = cast(str, credentials.conf('server'))
if not is_ipaddress(server):
raise errors.PluginError("The configured target DNS server ({0}) is not a valid IPv4 "
"or IPv6 address. A hostname is not allowed.".format(server))
Expand Down Expand Up @@ -89,11 +90,11 @@ def _cleanup(self, _domain: str, validation_name: str, validation: str) -> None:
def _get_rfc2136_client(self) -> "_RFC2136Client":
if not self.credentials: # pragma: no cover
raise errors.Error("Plugin has not been prepared.")
return _RFC2136Client(self.credentials.conf('server'),
int(self.credentials.conf('port') or self.PORT),
self.credentials.conf('name'),
self.credentials.conf('secret'),
self.ALGORITHMS.get(self.credentials.conf('algorithm'),
return _RFC2136Client(cast(str, self.credentials.conf('server')),
int(cast(str, self.credentials.conf('port')) or self.PORT),
cast(str, self.credentials.conf('name')),
cast(str, self.credentials.conf('secret')),
self.ALGORITHMS.get(self.credentials.conf('algorithm') or '',
dns.tsig.HMAC_MD5))


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import logging
from typing import Any
from typing import Callable
from typing import cast
from typing import Optional

from lexicon.providers import sakuracloud
Expand Down Expand Up @@ -64,8 +65,8 @@ def _get_sakuracloud_client(self) -> "_SakuraCloudLexiconClient":
if not self.credentials: # pragma: no cover
raise errors.Error("Plugin has not been prepared.")
return _SakuraCloudLexiconClient(
self.credentials.conf('api-token'),
self.credentials.conf('api-secret'),
cast(str, self.credentials.conf('api-token')),
cast(str, self.credentials.conf('api-secret')),
self.ttl
)

Expand Down
8 changes: 4 additions & 4 deletions certbot/certbot/plugins/dns_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,20 +312,20 @@ def require(self, required_variables: Mapping[str, str]) -> None:
)
)

def conf(self, var: str) -> str:
def conf(self, var: str) -> Optional[str]:
"""Find a configuration value for variable `var`, as transformed by `mapper`.
:param str var: The variable to get.
:returns: The value of the variable.
:rtype: str
:returns: The value of the variable, if it exists.
:rtype: str or None
"""

return self._get(var)

def _has(self, var: str) -> bool:
return self.mapper(var) in self.confobj

def _get(self, var: str) -> str:
def _get(self, var: str) -> Optional[str]:
return self.confobj.get(self.mapper(var))


Expand Down

0 comments on commit 399b932

Please sign in to comment.