Skip to content

Commit

Permalink
feat: Add cli for simple gateway registration
Browse files Browse the repository at this point in the history
Signed-off-by: Christine Wang <christinewang@fb.com>
  • Loading branch information
christinewang5 committed Mar 21, 2022
1 parent 641fcad commit 813b111
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ package unary
var identityDecoratorBypassList = map[string]struct{}{
"/magma.orc8r.Bootstrapper/GetChallenge": {},
"/magma.orc8r.Bootstrapper/RequestSign": {},
"/magma.orc8r.Registration/Register": {},
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,7 @@ func NewRegistrationServicer() protos.RegistrationServer {
}

func (r *RegistrationService) Register(c context.Context, request *protos.RegisterRequest) (*protos.RegisterResponse, error) {
nonce, err := NonceFromToken(request.Token)
if err != nil {
return nil, err
}

deviceInfo, err := r.GetGatewayDeviceInfo(context.Background(), nonce)
deviceInfo, err := r.GetGatewayDeviceInfo(context.Background(), request.Token)
if err != nil {
clientErr := makeErr(fmt.Sprintf("could not get device info from token %v: %v", request.Token, err))
return clientErr, nil
Expand Down
26 changes: 26 additions & 0 deletions orc8r/gateway/python/magma/common/service_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from magma.configuration.service_configs import load_service_config

GRPC_KEEPALIVE_MS = 30 * 1000
ROOTCA_PATH = '/var/opt/magma/certs/rootCA.pem'


class ServiceRegistry:
Expand Down Expand Up @@ -85,6 +86,31 @@ def reset():
"""
ServiceRegistry.get_registry()["services"] = {}

@staticmethod
def get_bare_bootstrap_rpc_channel(domain_name, port, rootca_path=None):
"""
Return an RPC channel to bootstrap service in CLOUD, without referencing a
control proxy config.
Returns:
grpc channel
"""
rootca_path = rootca_path or ROOTCA_PATH
with open(rootca_path, 'rb') as rootca_f:
rootca = rootca_f.read()
ssl_creds = grpc.ssl_channel_credentials(
root_certificates=rootca,
)

authority = 'bootstrapper-controller.%s' % domain_name
grpc_options = [('grpc.default_authority', authority)]
channel = grpc.secure_channel(
target='%s:%s' % (authority, port),
credentials=ssl_creds,
options=grpc_options,
)
return channel

@staticmethod
def get_bootstrap_rpc_channel():
"""
Expand Down
128 changes: 128 additions & 0 deletions orc8r/gateway/python/scripts/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
#!/usr/bin/env python3
"""
Copyright 2020 The Magma Authors.
This source code is licensed under the BSD-style license found in the
LICENSE file in the root directory of this source tree.
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import argparse
import sys
import textwrap
from typing import List

import checkin_cli
import snowflake
from magma.common.cert_utils import load_public_key_to_base64der
from magma.common.service_registry import ServiceRegistry
from magma.configuration.service_configs import save_override_config
from orc8r.protos.bootstrapper_pb2 import (
ChallengeKey,
RegisterRequest,
RegisterResponse,
)
from orc8r.protos.bootstrapper_pb2_grpc import RegistrationStub
from orc8r.protos.identity_pb2 import AccessGatewayID


def register_handler(client: RegistrationStub, args: List[str]) -> RegisterResponse:
"""
Register a device and retrieves its control proxy
Args:
client: Registration stub
args: command line arguments
Returns:
RegisterRequest: register request, used for printing after function returns
RegisterResponse: response from gRPC call, either error or the control_proxy
"""
req = RegisterRequest(
token=args.token,
hwid=AccessGatewayID(
id=snowflake.snowflake(),
),
challenge_key=ChallengeKey(
key=load_public_key_to_base64der("/var/opt/magma/certs/gw_challenge.key"),
key_type=ChallengeKey.KeyType.SOFTWARE_ECDSA_SHA256,
),
)

res = client.Register(req)
if res.HasField("error"):
raise Exception(res.error)

return req, res


def main():
"""Register a gateway"""
parser = argparse.ArgumentParser(description="Register a gateway.")
parser.add_argument(
"domain",
metavar="DOMAIN_NAME",
type=str,
help="orc8r's domain name",
)
parser.add_argument(
"token",
metavar="REGISTRATION_TOKEN",
type=str,
help="registration token after API call",
)
parser.add_argument(
"--ca-file",
type=str,
help="orc8r's root CA file",
)
parser.add_argument(
"--cloud-port",
type=str,
help="orc8r's port",
)
parser.add_argument(
"--no-control-proxy",
action="store_true",
help="disables writing the control proxy file",
)
args = parser.parse_args()
chan = ServiceRegistry.get_bare_bootstrap_rpc_channel(
args.domain,
'8444' if not args.cloud_port else args.cloud_port,
args.ca_file,
)
client = RegistrationStub(chan)
try:
req, res = register_handler(client, args)
msg = textwrap.dedent(
"""
> Registered gateway
Hardware ID
-----------
{}
Challenge Key
-----------
{}
Control Proxy
-----------
{}
""",
)
print(msg.format(req.hwid, req.challenge_key, res.control_proxy))
except Exception as e:
msg = textwrap.dedent(" > Error: {} ")
print(msg.format(e))
sys.exit(1)

if not args.no_control_proxy:
save_override_config("control_proxy", res.control_proxy)


if __name__ == "__main__":
main()
print("> Running checkin_cli")
checkin_cli.main()

0 comments on commit 813b111

Please sign in to comment.