Create a key

Reading or writing key definitions requires an admin key.

Problem

You need to create a key in the current database.

Solution

Use the CreateKey function:

try
{
    Value result = await adminClient.Query(
        CreateKey(
            Obj(
                "database", Database("prydain"),
                "role", "server"
            )
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "309749531319206400", collection = RefV(id = "keys")),ts: LongV(1631659022560000),database: RefV(id = "prydain", collection = RefV(id = "databases")),role: StringV(server),secret: StringV(fnAETHOOv6ACACCGJIDhYL9aXeLgMCbdn5zw1_Nq),hashed_secret: StringV($2a$05$Gu/nO7wdPDr8pAUZz1g3hOQQYPkLIqCkAUL1tcPWybJApBrEgmqj.))
result, err := adminClient.Query(
	f.CreateKey(
		f.Obj{
			"database": f.Database("prydain"),
			"role": "server",
		},
	),
)

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[database:{prydain 0xc0001845a0 0xc0001845a0 <nil>} hashed_secret:$2a$05$PYjJZWK/7jhiTNMYzevVP.Ci3HoaxYMUWKJSe7v/Kfd2pM0gNOxzy ref:{309750664624341504 0xc0001844b0 0xc0001844b0 <nil>} role:server secret:fnAETHSWneACAKCJlNYbBidugnzgQW7ZLe1WKuMe ts:1631660103360000]
System.out.println(
    adminClient.query(
        CreateKey(
            Obj(
                "database", Database("prydain"),
                "role", Value("server")
            )
        )
    ).get());
{ref: ref(id = "309752044938330624", collection = ref(id = "keys")), ts: 1631661419730000, database: ref(id = "prydain", collection = ref(id = "databases")), role: "server", secret: "fnAETHXX_wACAMC1JKCDyQlqF3N2xW7_mPF2C6Ap", hashed_secret: "$2a$05$6DEulAKI5LhXlPzHiKoskOsfwtiBWisrG2oXiBtqpO4RWKX51N9JO"}
adminClient.query(
  q.CreateKey({
    database: q.Database('prydain'),
    role: 'server',
  })
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{
  ref: Ref(Keys(), "309749247463391744"),
  ts: 1631658751850000,
  database: Database("prydain"),
  role: 'server',
  secret: 'fnAETHNMqIACALQjS6v19qW8M2_gg-JNgn7ygC16',
  hashed_secret: '$2a$05$1SJ9KBqMn.YHXJT0P2VZs.6YrRY0Qx41KL/2A8tAGjJ3bAC4o.L1u'
}
result = adminClient.query(
  q.create_key({
    "database": q.database("prydain"),
    "role": "server"
  })
)
print(result)
{'ref': Ref(id=309751197640688128, collection=Ref(id=keys)), 'ts': 1631660611680000, 'database': Ref(id=prydain, collection=Ref(id=databases)), 'role': 'server', 'secret': 'fnAETHUSuCACAIus1In2Tz3Rv4CIcYCMMzsiFGyM', 'hashed_secret': '$2a$05$gevYk..SvzS3U6cImfQhzuThKJFvlKZP/U89ZgAeK7cXiN5qztwzG'}
try {
  val result = Await.result(
    adminClient.query(
      CreateKey(
        Obj(
          "database" -> Database("prydain"),
          "role" -> "server"
        )
      )
    ),
    5.seconds
  )
  println(result)
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
{secret: "fnAETHVta-ACAJTuI9MkVonBUqfLygSJ2fZQSN5A", database: ref(id = "prydain", collection = ref(id = "databases")), role: "server", ts: 1631660983200000, ref: ref(id = "309751587203449344", collection = ref(id = "keys")), hashed_secret: "$2a$05$hZMQDRVJtxe6RmVpUj5sk.KAbKxsjGZQRKRm29XEhA.vo5HKpW7Iq"}
CreateKey({
  database: Database("prydain"),
  role: "server"
})
{
  ref: Ref(Keys(), "309732159785009664"),
  ts: 1631642455800000,
  database: Database("prydain"),
  role: 'server',
  secret: 'fnAETGPCH4ACAIf9avmbmbXAS7Yl-P-hDbw8QF9y',
  hashed_secret: '$2a$05$U6/orFxArR4o/LmMBabTwuD.SEkkujaxj9q4gb0wRhYP5AXLJNadW'
}
The key’s secret is only ever shown once, in the response from the CreateKey function. If you lose the secret, delete the key and create a new one.

More information

Was this article helpful?

We're sorry to hear that.
Tell us how we can improve!
Visit Fauna's Discourse forums or email docs@fauna.com

Thank you for your feedback!