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 Feb 9, 2022
1 parent 40524fb commit f2158bd
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 2 deletions.
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
4 changes: 2 additions & 2 deletions orc8r/cloud/docker/controller/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ stdout_events_enabled=true
stderr_events_enabled=true

[program:bootstrapper]
command=/usr/bin/envdir /var/opt/magma/envdir /var/opt/magma/bin/bootstrapper -cak=/var/opt/magma/certs/bootstrapper.key -logtostderr=true -v=0
command=/usr/bin/envdir /var/opt/magma/envdir /var/opt/magma/bin/bootstrapper -cak=/var/opt/magma/certs/bootstrapper.key -logtostderr=true -v=10
autorestart=true
stdout_logfile=NONE
stderr_logfile=NONE
Expand Down Expand Up @@ -143,7 +143,7 @@ stdout_events_enabled=true
stderr_events_enabled=true

[program:obsidian]
command=/usr/bin/envdir /var/opt/magma/envdir /var/opt/magma/bin/obsidian -logtostderr=true -v=0
command=/usr/bin/envdir /var/opt/magma/envdir /var/opt/magma/bin/obsidian -logtostderr=true -v=10
autorestart=true
stdout_logfile=NONE
stderr_logfile=NONE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/go-openapi/strfmt"
"github.com/golang/glog"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"

Expand Down Expand Up @@ -33,25 +34,30 @@ func NewRegistrationServicer() protos.RegistrationServer {
}

func (r *RegistrationService) Register(c context.Context, request *protos.RegisterRequest) (*protos.RegisterResponse, error) {
glog.Errorf("christinewang5 --- in Register function")
nonce, err := NonceFromToken(request.Token)
if err != nil {
glog.Errorf("christinewang5 --- NonceFromToken %v", err)
return nil, err
}

deviceInfo, err := r.GetGatewayDeviceInfo(context.Background(), nonce)
if err != nil {
glog.Errorf("christinewang5 --- r.GetGatewayDeviceInfo %v", err)
clientErr := makeErr(fmt.Sprintf("could not get device info from token %v: %v", request.Token, err))
return clientErr, nil
}

err = r.RegisterDevice(*deviceInfo, request.Hwid, request.ChallengeKey)
if err != nil {
glog.Errorf("christinewang5 --- r.RegisterDevice %v", err)
clientErr := makeErr(fmt.Sprintf("error registering device: %v", err))
return clientErr, nil
}

controlProxy, err := r.GetControlProxy(deviceInfo.NetworkId)
if err != nil {
glog.Errorf("christinewang5 --- r.GetControlProxy %v", err)
clientErr := makeErr(fmt.Sprintf("error getting control proxy: %v", err))
return clientErr, nil
}
Expand Down
90 changes: 90 additions & 0 deletions orc8r/gateway/python/scripts/register.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
#!/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"

@cloud_grpc_wrapper
def register_handler(client, args):
req = RegisterRequest()
req.token = args.token
req.hwid.id = snowflake.snowflake()
req.challenge_key.key = load_public_key_to_base64der("/var/opt/magma/certs/gw_challenge.key")
req.challenge_key.key_type = 0
# TODO: what is req.challenge_key.key_type?
print("christinewang5 req \n--- \n", req)

res = client.Register(req)
print("christinewang5 res \t---\t", res)

print("Successfully registered gateway")
print( "Hardware ID\n-----------\n{}\n", req.hwid)
print( "Challenge Key\n-----------\n{}\n", req.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)
print("what is happening %s", register_res)
# 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

0 comments on commit f2158bd

Please sign in to comment.