Repository: rotki/rotki

Issue:
Kraken setup
## Problem Definition

Adding Kraken's API credentials fails and then I'm unable to set it up.
<img width="713" alt="Screen Shot 2019-08-04 at 10 01 24" src="https://user-images.githubusercontent.com/2582555/62421238-04514100-b69f-11e9-84b7-0a80e3624cce.png">
<img width="295" alt="Screen Shot 2019-08-04 at 10 00 09" src="https://user-images.githubusercontent.com/2582555/62421239-04e9d780-b69f-11e9-8d0c-b8c795248b9e.png">


### System Description

Here add a detailed description of your system, e.g. output of the following script:

```
Darwin Inos-MacBook-Pro.local 18.2.0 Darwin Kernel Version 18.2.0: Thu Dec 20 20:46:53 PST 2018; root:xnu-4903.241.1~1/RELEASE_X86_64 x86_64

rotkehlchenio 1.0.1
```



Discussion:
Logging out and back in "kinda" solved the problem but then I get:
```
Error at setup of kraken: EGeneral:Permission denied
```

Edit: `Query Funds`needs to be enabled on Kraken API.

Still, this I think could be vocalized better and if an exchange provisioning process is faulty, the app shouldn't "register" it. 
The main reason adding an api key fails would be that it's not a valid api key. Are you sure you copied it correctly?

If you get a permission denied then this may mean that you have copy pasted the api key wrong.

Let me try to reproduce your issue as I also have a kraken api key.
Found another one:
Immediately after you successfully provision a new exchange, the left dashboard button `Exchange Trades` stops being responsive.  

Edit:
Clicking on `OTC Trades`, brings up
```DataTables warning: table id=table_otctrades - Cannot reinitialise DataTable. For more information about this error, please see http://datatables.net/tn/3```
@InoMurko The Exchange trades button is actually a "TODO" It does not work yet. There is an issue for this: https://github.com/rotkehlchenio/rotkehlchen/issues/284
@InoMurko So can you tell me does adding a kraken api key work for you? I just tried it on a new clean account and it worked without problems using the latest master. But it should generally work.

As I said [here](https://github.com/rotkehlchenio/rotkehlchen/issues/442#issuecomment-517983023) the only reason I can imagine this may not work is that the key and the secret (private key) combination is invalid.
What it was is actually (https://github.com/rotkehlchenio/rotkehlchen/issues/442#issuecomment-517982929):
Query Funds needs to be enabled on Kraken API. Still, this I think could be vocalized better and if an exchange  provisioning process is faulty, the app shouldn't "register" it.

I agree with your comment! This behaviour of kraken API is new. I will see what I can do. Thank you for opening an issue for this.

Resolving patch:
diff --git a/rotkehlchen/kraken.py b/rotkehlchen/kraken.py
--- a/rotkehlchen/kraken.py
+++ b/rotkehlchen/kraken.py
@@ -264,16 +264,57 @@ def first_connection(self):
             self.first_connection_made = True
 
     def validate_api_key(self) -> Tuple[bool, str]:
+        """Validates that the Kraken API Key is good for usage in Rotkehlchen
+
+        Makes sure that the following permission are given to the key:
+        - Ability to query funds
+        - Ability to query open/closed trades
+        - Ability to query ledgers
+        """
+        valid, msg = self._validate_single_api_key_action('Balance')
+        if not valid:
+            return False, msg
+        valid, msg = self._validate_single_api_key_action(
+            method_str='TradesHistory',
+            req={'start': 0, 'end': 0},
+        )
+        if not valid:
+            return False, msg
+        valid, msg = self._validate_single_api_key_action(
+            method_str='Ledgers',
+            req={'start': 0, 'end': 0, 'type': 'deposit'},
+        )
+        if not valid:
+            return False, msg
+        return True, ''
+
+    def _validate_single_api_key_action(
+            self,
+            method_str: str,
+            req: Optional[Dict[str, Any]] = None,
+    ) -> Tuple[bool, str]:
         try:
-            self.query_private('Balance', req={})
+            self.query_private(method_str, req)
         except (RemoteError, ValueError) as e:
             error = str(e)
             if 'Error: Incorrect padding' in error:
                 return False, 'Provided API Key or secret is in invalid Format'
             elif 'EAPI:Invalid key' in error:
                 return False, 'Provided API Key is invalid'
+            elif 'EGeneral:Permission denied' in error:
+                msg = (
+                    'Provided API Key does not have appropriate permissions. Make '
+                    'sure that the "Query Funds", "Query Open/Closed Order and Trades"'
+                    'and "Query Ledger Entries" actions are allowed for your Kraken API Key.'
+                )
+                return False, msg
             else:
-                raise
+                log.error(f'Kraken API key validation error: {str(e)}')
+                msg = (
+                    'Unknown error at Kraken API key validation. Perhaps API '
+                    'Key/Secret combination invalid?'
+                )
+                return False, msg
         return True, ''
 
     def _query_public(self, method: str, req: Optional[dict] = None) -> dict:


Test patch: