blob: 10382398e3aa70b23f6292297cead22cedfe5e1a [file] [log] [blame]
Jiri Simsad7616c92015-03-24 23:44:30 -07001// Copyright 2015 The Vanadium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
Todd Wang8c4e5cc2015-04-09 11:30:52 -07005// Package identitylib implements a test identityd service under the
6// v.io/x/ref/test/modules framework.
Todd Wang08265992015-04-06 16:45:20 -07007package identitylib
Nicolas LaCassec7cdf422015-01-08 14:11:29 -08008
9import (
10 "flag"
11 "fmt"
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080012 "net"
13 "strconv"
14 "time"
15
Jiri Simsa6ac95222015-02-23 16:11:49 -080016 "v.io/v23"
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080017
Suharsh Sivakumarc0048112015-03-19 11:48:28 -070018 "v.io/x/ref/services/identity/internal/auditor"
19 "v.io/x/ref/services/identity/internal/blesser"
20 "v.io/x/ref/services/identity/internal/caveats"
21 "v.io/x/ref/services/identity/internal/oauth"
22 "v.io/x/ref/services/identity/internal/revocation"
23 "v.io/x/ref/services/identity/internal/server"
24 "v.io/x/ref/services/identity/internal/util"
Cosmos Nicolaou1381f8a2015-03-13 09:40:34 -070025 "v.io/x/ref/test/modules"
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080026)
27
28var (
Suharsh Sivakumar510d43f2015-04-01 18:13:26 -070029 externalHttpAddr = flag.String("external-http-addr", "", "External address on which the HTTP server listens on. If none is provided the server will only listen on -http-addr.")
30 httpAddr = flag.CommandLine.String("http-addr", "localhost:0", "Address on which the HTTP server listens on.")
31 tlsConfig = flag.CommandLine.String("tls-config", "", "Comma-separated list of TLS certificate and private key files. This must be provided.")
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080032)
33
Todd Wang95873902015-05-22 14:21:30 -070034var TestIdentityd = modules.Register(func(env *modules.Env, args ...string) error {
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080035 // Duration to use for tls cert and blessing duration.
36 duration := 365 * 24 * time.Hour
37
Jiri Simsa6ac95222015-02-23 16:11:49 -080038 ctx, shutdown := v23.Init()
Suharsh Sivakumar9d17e4a2015-02-02 22:42:16 -080039 defer shutdown()
40
Suharsh Sivakumar510d43f2015-04-01 18:13:26 -070041 // If no tls-config has been provided, generate new cert and key and use them.
42 if flag.CommandLine.Lookup("tls-config").Value.String() == "" {
Suharsh Sivakumar4a30f212015-03-25 17:03:42 -070043 addr := *externalHttpAddr
44 if *externalHttpAddr == "" {
45 addr = *httpAddr
46 }
47 host, _, err := net.SplitHostPort(addr)
48 if err != nil {
Bogdan Caprita013b1062015-05-06 17:15:15 -070049 return fmt.Errorf("Failed to parse %q: %v", addr, err)
Suharsh Sivakumar4a30f212015-03-25 17:03:42 -070050 }
51 certFile, keyFile, err := util.WriteCertAndKey(host, duration)
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080052 if err != nil {
53 return fmt.Errorf("Could not write cert and key: %v", err)
54 }
Suharsh Sivakumar510d43f2015-04-01 18:13:26 -070055 if err := flag.CommandLine.Set("tls-config", certFile+","+keyFile); err != nil {
56 return fmt.Errorf("Could not set tls-config: %v", err)
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080057 }
58 }
59
Suharsh Sivakumar510d43f2015-04-01 18:13:26 -070060 // Pick a free port if http-addr flag is not set.
Suharsh Sivakumar9d17e4a2015-02-02 22:42:16 -080061 // We can't use :0 here, because the identity server calls
62 // http.ListenAndServeTLS, which blocks, leaving us with no way to tell
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080063 // what port the server is running on. Hence, we must pass in an
64 // actual port so we know where the server is running.
Suharsh Sivakumar510d43f2015-04-01 18:13:26 -070065 if flag.CommandLine.Lookup("http-addr").Value.String() == flag.CommandLine.Lookup("http-addr").DefValue {
66 if err := flag.CommandLine.Set("http-addr", "localhost:"+freePort()); err != nil {
67 return fmt.Errorf("Could not set http-addr: %v", err)
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080068 }
69 }
70
Ankurcb02c522015-08-18 19:27:43 -070071 mockClientID := "test-client-id"
72 mockClientName := "test-client"
73
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080074 auditor, reader := auditor.NewMockBlessingAuditor()
Cosmos Nicolaoud9229922015-06-24 14:12:24 -070075 revocationManager := revocation.NewMockRevocationManager(ctx)
Ankurcb02c522015-08-18 19:27:43 -070076 oauthProvider := oauth.NewMockOAuth("testemail@example.com", mockClientID)
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080077
Ankur123a5c72015-01-12 16:03:43 -080078 params := blesser.OAuthBlesserParams{
79 OAuthProvider: oauthProvider,
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080080 BlessingDuration: duration,
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080081 RevocationManager: revocationManager,
Ankurcb02c522015-08-18 19:27:43 -070082 AccessTokenClients: []oauth.AccessTokenClient{
83 oauth.AccessTokenClient{
84 Name: mockClientName,
85 ClientID: mockClientID,
86 },
87 },
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080088 }
89
90 s := server.NewIdentityServer(
Ankur123a5c72015-01-12 16:03:43 -080091 oauthProvider,
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080092 auditor,
93 reader,
94 revocationManager,
Ankur123a5c72015-01-12 16:03:43 -080095 params,
Asim Shankarc195b6d2015-02-03 11:26:55 -080096 caveats.NewMockCaveatSelector(),
Robin Thellend1b3c7d82015-03-26 13:52:37 -070097 "",
98 "identity")
Nicolas LaCassec7cdf422015-01-08 14:11:29 -080099
Jiri Simsa6ac95222015-02-23 16:11:49 -0800100 l := v23.GetListenSpec(ctx)
Nicolas LaCassec7cdf422015-01-08 14:11:29 -0800101
Suharsh Sivakumar510d43f2015-04-01 18:13:26 -0700102 _, eps, externalHttpAddress := s.Listen(ctx, &l, *externalHttpAddr, *httpAddr, *tlsConfig)
Nicolas LaCassec7cdf422015-01-08 14:11:29 -0800103
Todd Wang95873902015-05-22 14:21:30 -0700104 fmt.Fprintf(env.Stdout, "TEST_IDENTITYD_NAME=%s\n", eps[0])
105 fmt.Fprintf(env.Stdout, "TEST_IDENTITYD_HTTP_ADDR=%s\n", externalHttpAddress)
Nicolas LaCassec7cdf422015-01-08 14:11:29 -0800106
Todd Wang95873902015-05-22 14:21:30 -0700107 modules.WaitForEOF(env.Stdin)
Nicolas LaCassec7cdf422015-01-08 14:11:29 -0800108 return nil
Todd Wang95873902015-05-22 14:21:30 -0700109}, "TestIdentityd")
Nicolas LaCassec7cdf422015-01-08 14:11:29 -0800110
111func freePort() string {
112 l, _ := net.Listen("tcp", ":0")
113 defer l.Close()
114 return strconv.Itoa(l.Addr().(*net.TCPAddr).Port)
115}