2

I'm trying to use the Create Datasource REST endpoint from Microsoft's Power BI API, but I'm getting a 400 error when I send the encrypted string of my Windows credentials. My API is written in nodejs, so I'm using node-rsa to replicate the functionality from the C# solution in their docs.

I generated an encrypted string of my credentials with this C# code and it works fine and returns 201 for creating the Datasource:

var credentials = new WindowsCredentials(username: "myusername", password: "mypassword");

var publicKey = new GatewayPublicKey("<exponent>", "<modulus>");
            
var credentialsEncryptor = new AsymmetricKeyEncryptor(publicKey);
var credentialDetails = new CredentialDetails(credentials, PrivacyLevel.Organizational, EncryptedConnection.Encrypted, credentialsEncryptor);

This is the encrypted string from the Credentials prop of the credentialDetails object in the code above: oavtaHCi2Nkn0euRQxtRvZ2aDPnLA5/T1B6YHHeQzfBDXLJsU4JOx3ZOm31nEWqPRUh2DLARKI0y+C106wgOhWKgxI4nIz5jmJCQqPWRzOYlJlvdPQCOrTtuvIZb3IRy2OAhBfKOcOA8xqHIkKsqvTX1u4LUgfNuwxmU16FB/d3mzZOtc3+ld2MUp15HEPBCen6qTOkdEOwOm2AqxSidx46xoahLrqjkMtLZqLimooVGeTmuXm7bwoRQa1VKUwPaAuKcBBfAuRJulXyeSOMkwKz90pA30y8g7aB2NvT+92SzzjpuHcs1bzPrbPR1J+WXSRTN7xzlCOyQmC5YiQKn6A==AADPIC5L5rjd7WWThtCNAPunINW330ka1ts53SsBkaelIrZ8f9msgE4JjliaeDM91Qq3KDqm1DUjLw6Fg92LzZhMkjcwdEBued/piCdvIpP0eA9rhX0kjWWaMzQF6T3fHr798OkL7yMPEGi+m7Z3cOz22HP2Ot1ORxf6RH/JUNJmLEepEYCRVVubmKL04IyEtx8dShtSG+upeOaBOaD/GOS5


This is my nodejs solution using node-rsa. Having trouble understanding why the encrypted string from the C# code above works, but this one doesn't.

const nodersa = require('node-rsa');

const credentials =
    '{"credentialData":[{"name":"username", "value":"myusername"},{"name":"password", "value":"mypassword"}]}';

const exponentString = '<exponent>';
const modulusString = '<modulus>';

const key = new nodersa();

key.setOptions({
    encryptionScheme: 'pkcs1_oaep',
});

const pubKey = key.importKey({
    n: Buffer.from(modulusString, 'base64'),
    e: Buffer.from(exponentString, 'base64'),
});

const encrypted = pubKey.encrypt(credentials, 'base64');

This is the encrypted string from this code above: UCLIIGF8u6HMdlLAwVeZcM0iLks5YLEWhRfpywnNBrAdSoPlP8/vRqe4knMCnAFiSimHiX8fu/CQlAP0b98Xlzx6sHnW4sQHV5KgUErLQfBP5i+5LGj7lBDB0/nsuf2hLDSrXXUb3F+XXv1mlUTgNZzwanizUuRcqYRxcdMaYOjI1vCaQbW++kHXMgOQPjMBj8hrJ1gW1WznS3zCYy6v8oUwEzJtp2aQEP8Pycvx5KwjVdy9KxkB675+TfddauMlz0B+EpQjC1Z+k87uiuFpGZxwA5FAi+4r8ztZ+9/su+8eieCSBK/8ZokAUNcrLmU0sPuDewaojW1MBdxvJiv7YA==


This is the error that Power BI API returns using string encrypted via node solution. Everything else about my request is identical except the encrypted creds, so I'm confident that that is the issue.

{
  "error": {
    "code": "DM_GWPipeline_UnknownError",
    "pbi.error": {
      "code": "DM_GWPipeline_UnknownError",
      "parameters": {},
      "details": [
        {
          "code": "DM_ErrorDetailNameCode_UnderlyingErrorMessage",
          "detail": {
            "type": 1,
            "value": "The parameter is incorrect.\r\n"
          }
        },
        {
          "code": "DM_ErrorDetailNameCode_UnderlyingHResult",
          "detail": {
            "type": 1,
            "value": "-2146893785"
          }
        }
      ],
      "exceptionCulprit": 1
    }
  }
}
ohcalcutta
  • 21
  • 1
  • I am experiencing the same problem right now - did you ever resolve this? I have attempted multiple crypto libraries and none of them have worked. I'm not sure why MS doesn't just provide the public key instead of the modulus and exponent. – Kevin Jhangiani May 25 '21 at 20:57
  • I didn't, unfortunately. I just went with the C# solution because I was under the gun for meeting a deadline. MS's support for the PowerBI API is lacking, to put it mildly. – ohcalcutta May 26 '21 at 21:31
  • Could you point me to the C# solution? I am running into this exact issue except using powershell. – Aaron L. Jun 18 '21 at 23:29
  • The C# solution is stubbed out in the first code block from the OP – ohcalcutta Jun 21 '21 at 18:38

1 Answers1

0

You can fix this by upgrading to the latest RSA encryption that supports 2048 bits.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Vaisu
  • 1
  • 1
    Do you have any insight on how to do this in node.js? I've tried a number of different libraries and options, but cannot get the encryption to work in node – Kevin Jhangiani Jul 21 '21 at 23:16
  • Same here, can't get it to work. Adding `key.generateKeyPair();` after `const key = new nodeRSA();` is supposed to upgrade to 2048 bit, the output changes, but Power BI keeps throwing the same error – daniel p Aug 24 '21 at 18:09