3

I have created a type provider to a service that requires authentication by user name and password. The signature for creating the typed instance is something like this:

let tbl = new DemoTable<TableId="demo",User="user",Password="password">()

It all works just fine, the only problem is that I want to demonstrate this code, so I'd like the user name and password to come from some other source - a configuration file, another F# module (good approach when using scripting), etc. What are my options for passing these parameters so they are not exposed in the course of demonstration?

3 Answers 3

5

One simple way is to use literals in a file that you don't show:

module MyInfo =
    [<Literal>]
    let user = "user name here"

    [<Literal>]
    let pwd = "password here"

And then in the file that you do show:

let tbl = new DemoTable<TableId="demo",User=MyInfo.user,Password=MyInfo.pwd>()
1
  • +1 This is a nice trick! I think the main difference between this and using a config file is that the name & password will actually be in the compiled binary if you use this (whereas it won't be there if you use config file). This might matter in some deployment scenarios, but probably not in the scripting case. Apr 22, 2014 at 12:50
4

Specifying the user name and password directly as static parameters is certainly useful in interactive scripting scenarios. But as you say, sometimes it is also useful to keep those in a separate configuration file. F# does not come with any library that would make this easier, but you can implement the same pattern as, for example, the SqlDataConnection type provider which provides both options (see MSDN docs).

You can either specify the whole connection string:

type DB = SqlDataConnection<ConnectionString="Source=.; User=me; Password=mysecret">

or can either specify local config file and key name:

type DB = SqlDataConnection<ConnectionStringName="LocalDb", ConfigFile="App.config">

This is something that you'd have to implement in your type provider, but it seems like the best way to support both scenarios.

2
  • If I use connection string pattern, I still expose my password. I can's write SqlDataConnection<ConnectionString=myConnection>()... Config is also not ideal - if I use scripting, there is no config. Apr 21, 2014 at 17:45
  • 1
    You can still use configuration file in the scripting scenario - the standard SQL type provider just looks for the config file of the specified name in the same folder where your script file is. You could do the same for your type provider. (But you still have to have the config file there, so the password is still stored somewhere.) Apr 21, 2014 at 17:57
0

Change password on database (or make temporary user)->compile project->rechange password (or delete user).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.