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 Jan 18, 2022
1 parent 0a5ed3b commit 18a5a5d
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cwf/k8s/cwf_operator/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.3 h1:gyjaxf+svBWX08ZjK86iN9geUJF0H6gp2IRKX6Nf6/I=
github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/snappy v0.0.0-20170215233205-553a64147049/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
Expand Down Expand Up @@ -1473,6 +1474,8 @@ google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk=
google.golang.org/grpc v1.27.1/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.31.0 h1:T7P4R73V3SSDPhH7WW7ATbfViLtmamH0DKrP3f9AuDI=
google.golang.org/grpc v1.31.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw=
gopkg.in/airbrake/gobrake.v2 v2.0.9/go.mod h1:/h5ZAUhDkGaJfjzjKLSjv6zCL6O0LLBxU4K+aSYdM/U=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
Expand Down
1 change: 1 addition & 0 deletions feg/gateway/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ github.com/jtolds/gls v4.2.1+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVY
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/jxskiss/base62 v1.0.0 h1:bvuMdKQF7C0L5qAr34Q2sYhhg+NZCObNSc2ecp4YMRQ=
github.com/jxskiss/base62 v1.0.0/go.mod h1:a5Mn24iYVJRUQSkFupGByqykzD+k+wFI8J91zGHuPf8=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQLJ+jE2L00=
Expand Down
89 changes: 89 additions & 0 deletions orc8r/gateway/python/scripts/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/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 grpc
import snowflake

from magma.common.cert_utils import load_public_key_to_base64der
from magma.common.rpc_utils import cloud_grpc_wrapper

from orc8r.protos.bootstrapper_pb2 import (
RegisterRequest,
GetGatewayDeviceInfoRequest,
)
from orc8r.protos.bootstrapper_pb2_grpc import (
RegistrationStub, # Register
CloudRegistrationStub, # GetGatewayDeviceInfo
)

BOOTSTRAPPER_SERVICE_NAME = "bootstrapper"

# TODO(christinewang5): is the AGW supposed to call this, i'm confused where to do this?
@cloud_grpc_wrapper
def register_handler(client, args):
register_request = RegisterRequest()
register_request.token = args.token

register_request.hwid.id = snowflake.snowflake()

public_key = load_public_key_to_base64der("/var/opt/magma/certs/gw_challenge.key")
register_request.challenge_key.key = str.encode(public_key.decode('utf-8'))
#TODO(christinewang) what is register_request.challenge_key.key_type

res = client.Register(register_request)
print("Successfully registered gateway")
print( "Hardware ID\n-----------\n{}\n", register_request.hwid)
print( "Challenge Key\n-----------\n{}\n", register_request.challenge_key)
# NOTE: returns oneof(err, control_proxy)
return res

@cloud_grpc_wrapper
def get_gateway_device_info_handler(client, args):
device_info_request = GetGatewayDeviceInfoRequest()
device_info_request.token = args.token
res = client.GetGatewayDeviceInfo(device_info_request)
print(res)
print("Successfully retrieved gateway device info") # TODO(christinewang): maybe print nicer messages
# NOTE: returns oneof(GatewayDeviceInfo, error)
# GatewayDeviceInfo: network_id, logical_id
return 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"
)
args = parser.parse_args()
# register_res = register_handler(args, RegistrationStub, BOOTSTRAPPER_SERVICE_NAME)
device_info_res = get_gateway_device_info_handler(args, CloudRegistrationStub, BOOTSTRAPPER_SERVICE_NAME)

if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions orc8r/gateway/python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
'scripts/magma_conditional_service.py',
'scripts/magma_get_config.py',
'scripts/magmad_cli.py',
'scripts/register.py',
'scripts/service_util.py',
'scripts/service303_cli.py',
'scripts/show_gateway_info.py',
Expand Down
2 changes: 1 addition & 1 deletion orc8r/tools/fab/dev_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def is_hw_id_registered(
# Handle paginated or flat response
gateways = paginated_gateways.get('gateways', paginated_gateways)
for gw in gateways.values():
if gw['device']['hardware_id'] == hw_id:
if 'device' in gw and gw['device']['hardware_id'] == hw_id:
return True, gw['id']
return False, ''

Expand Down

0 comments on commit 18a5a5d

Please sign in to comment.