summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-02-14 12:36:16 +0100
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-04-05 12:26:11 +0200
commit79afcbe57ab0b83c75d44004e6c75c309540448e (patch)
tree6202ec06ee6ff1d81fda9370e67763c432bd5952
parentd306d7b27ee1df60bf32f03b6160856e78d1b02c (diff)
Abstract error handling in python code
Signed-off-by: jwijenbergh <jeroenwijenbergh@protonmail.com>
-rw-r--r--src/server.go8
-rw-r--r--wrappers/python/eduvpncommon/discovery.py61
2 files changed, 42 insertions, 27 deletions
diff --git a/src/server.go b/src/server.go
index e39f4c7..7fe3735 100644
--- a/src/server.go
+++ b/src/server.go
@@ -1,9 +1,9 @@
package eduvpn
import (
+ "fmt"
"io/ioutil"
"net/http"
- "fmt"
)
func getFileUrl(url string) ([]byte, error) {
@@ -24,7 +24,7 @@ func getFileUrl(url string) ([]byte, error) {
if readErr != nil {
return nil, detailedVPNError{errRequestFileReadError, fmt.Sprintf("error reading body from file url %s", url), readErr}
}
- return body, nil
+ return body, nil
}
// Helper function that gets a disco json
@@ -64,7 +64,7 @@ func getDiscoFile(jsonFile string) (string, error) {
func GetOrganizationsList() (string, error) {
body, err := getDiscoFile("organization_list.json")
if err != nil {
- return "", err.(detailedRequestError).ToVerifyError()
+ return "", err.(detailedRequestError).ToRequestError()
}
return body, nil
}
@@ -77,6 +77,7 @@ func GetServersList() (string, error) {
// RequestErrorCode Simplified error code for public interface.
type RequestErrorCode = VPNErrorCode
type RequestError = VPNError
+
// detailedRequestErrorCode used for unit tests.
type detailedRequestErrorCode = detailedVPNErrorCode
type detailedRequestError = detailedVPNError
@@ -103,7 +104,6 @@ func (code detailedRequestErrorCode) ToRequestErrorCode() RequestErrorCode {
case errRequestFileReadError:
case errRequestFileHTTPError:
return ErrRequestFileError
- return ErrRequestFileError
case errVerifySigError:
return ErrVerifySigError
}
diff --git a/wrappers/python/eduvpncommon/discovery.py b/wrappers/python/eduvpncommon/discovery.py
index d7fa73b..ae721c7 100644
--- a/wrappers/python/eduvpncommon/discovery.py
+++ b/wrappers/python/eduvpncommon/discovery.py
@@ -10,22 +10,49 @@ lib.FreeString.argtypes, lib.FreeString.restype = [c_void_p], None
lib.Verify.argtypes, lib.Verify.restype = [GoSlice, GoSlice, GoSlice, c_uint64], c_int64
lib.InsecureTestingSetExtraKey.argtypes, lib.InsecureTestingSetExtraKey.restype = [GoSlice], None
-class RequestErrorCode(Enum):
- ErrRequestFileError = 1 # The request for the file has failed.
- ErrVerifySigError = 2 # The signature failed to verify.
- Unknown = -1 # Other unknown error.
-
-def getOrganizationsList():
+def getOrganizationsList() -> str:
dataError = lib.GetOrganizationsList()
ptr = dataError.data
- err = dataError.error
+ error = dataError.error
body = None
- if not err:
+ if not error:
body = cast(ptr, c_char_p).value
lib.FreeString(ptr)
+ if error:
+ raise RequestError(error)
return body
+class GoError(Exception):
+ message_dict: Enum
+ code: int
+
+ def __init__(self, err: Enum, messages: dict):
+ assert err
+ try:
+ self.code = err
+ except ValueError:
+ self.code = -1
+ self.message_dict = messages
+
+ def __str__(self):
+ return self.message_dict[self.code] if self.code in self.message_dict else f"unknown error ({self.code})"
+
+
+class RequestErrorCode(Enum):
+ ErrRequestFileError = 1 # The request for the file has failed.
+ ErrVerifySigError = 2 # The signature failed to verify.
+ Unknown = -1 # Other unknown error.
+
+class RequestError(GoError):
+ def __init__(self, err: int):
+ super().__init__(RequestErrorCode(err),
+ {
+ RequestErrorCode.ErrRequestFileError: "file request error",
+ RequestErrorCode.ErrVerifySigError: "signature verify error",
+ })
+
+
class VerifyErrorCode(Enum):
ErrUnknownExpectedFileName = 1 # Unknown expected file name specified. The signature has not been verified.
ErrInvalidSignature = 2 # Signature is invalid (for the expected file type).
@@ -33,27 +60,15 @@ class VerifyErrorCode(Enum):
ErrTooOld = 4 # Signature timestamp smaller than specified minimum signing time (rollback).
Unknown = -1 # Other unknown error.
-
-class VerifyError(Exception):
- code: VerifyErrorCode
- code_int: int # Original error code also for VerifyErrorCode.Unknown
-
+class VerifyError(GoError):
def __init__(self, err: int):
- assert err
- try:
- self.code = VerifyErrorCode(err)
- except ValueError:
- self.code = VerifyErrorCode.Unknown
- self.code_int = err
-
- def __str__(self):
- return \
+ super().__init__(VerifyErrorCode(err),
{
VerifyErrorCode.ErrUnknownExpectedFileName: "unknown expected file name",
VerifyErrorCode.ErrInvalidSignature: "invalid signature",
VerifyErrorCode.ErrInvalidSignatureUnknownKey: "invalid signature (unknown key)",
VerifyErrorCode.ErrTooOld: "replay of previous signature (rollback)",
- }[self.code] if self.code != VerifyErrorCode.Unknown else f"unknown verify error ({self.code_int})"
+ })
def verify(signature: bytes, signed_json: bytes, expected_file_name: str, min_sign_time: int) -> None: