Get a key

Reading or writing key definitions requires an admin key.

Problem

You need to retrieve the document that describes a key from the current database.

Solution

Use the Get function:

try
{
    Value result = await adminClient.Query(
        Get(Ref(Keys(), "1"))
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "1", collection = RefV(id = "keys")),ts: LongV(1631659915870000),role: StringV(admin),hashed_secret: StringV($2a$05$5Z1aSUffhrS5cgUDweRGc.s5X/nBCXYX9Dczyb.uKIfvBgjsioAvG))
result, err := adminClient.Query(
	f.Get(f.Ref(f.Keys(), "1")))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[hashed_secret:$2a$05$qjmTLEkdvEvAMBILQOYvyuCCoRCfO7lt/VqVvC3Ga8vE9enuzbkXy ref:{1 0xc000200240 0xc000200240 <nil>} role:admin ts:1631660100530000]
System.out.println(
    adminClient.query(
        Get(Ref(Keys(), "1"))
    ).get());
{ref: ref(id = "1", collection = ref(id = "keys")), ts: 1631660385310000, role: "admin", hashed_secret: "$2a$05$4XsUiJsdsd6zLN8kJF7yru3bHUONLzoiq0MpYYSkTJCXn6As3ThaG"}
adminClient.query(
  q.Get(q.Ref(q.Keys(), '1'))
)
.then((ret) => console.log(ret))
.catch((err) => console.error('Error: %s', err))
{
  ref: Ref(Keys(), "1"),
  ts: 1631658750650000,
  role: 'admin',
  hashed_secret: '$2a$05$Df1vv7hVWEJO0./ah1uMnOx6Jas0bAXr9BKzG8tvhFLfU6rotOxIG'
}
result = adminClient.query(
  q.get(q.ref(q.keys(), "1"))
)
print(result)
{'ref': Ref(id=1, collection=Ref(id=keys)), 'ts': 1631660773940000, 'role': 'admin', 'hashed_secret': '$2a$05$flBxAwoVCmMhazskpS3G1OqTc0da0Yf9FgH71r7Z4GmH1E4UbwXe6'}
try {
  println(Await.result(
    adminClient.query(
      Get(Ref(Keys(), "1"))
    ),
    5.seconds
  ))
} catch {
  case unknown: Throwable => println("Error: " + unknown.getMessage())
}
{ref: ref(id = "1", collection = ref(id = "keys")), ts: 1631660963070000, role: "admin", hashed_secret: "$2a$05$Huf8m8l1diWLImIalbUlM.sOcEGT9fg6TJL85dW9.STpwugOsUd7S"}
Get(Ref(Keys(), "1"))
{
  ref: Ref(Keys(), "1"),
  ts: 1630713320110000,
  role: 'admin',
  hashed_secret: '$2a$05$FZsfn143PoEKFTu/LsLJaOAL9cFsXRKfeRGb4r8UbGLN2.xO86.FG'
}

Discussion

Once a key has been created, its secret is only displayed at that time. Afterwards, it is no longer possible to retrieve the key’s secret. If you have lost the secret associated with a key, 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!