-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from openziti/add-appetizer-reflect-demo
Add appetizer reflect demo
- Loading branch information
Showing
10 changed files
with
143 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
Copyright NetFoundry Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
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. | ||
*/ | ||
|
||
using OpenZiti.Management; | ||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
using OpenZiti.NET.Samples.Common; | ||
using System.Security.Principal; | ||
using System.Reflection.PortableExecutable; | ||
using System.Net.Sockets; | ||
using OpenZiti.NET.Samples.src.Common; | ||
|
||
namespace OpenZiti.NET.Samples.Appetizer { | ||
|
||
[Sample("appetizer-reflect")] | ||
public class AppetizerSample : SampleBase { | ||
private static readonly NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger(); | ||
internal const string REFLECT_SERVICE_NAME = "reflectService"; | ||
|
||
public override async Task<object> RunAsync(string[] args) { | ||
Log.Info("Appetizer reflect demo starts"); | ||
var zitiContext = AppetizerSetup.ContextFromFile(args[1]); | ||
using Stream stream = ZitifiedNetworkStream.NewStream(zitiContext, REFLECT_SERVICE_NAME, null); | ||
using var reader = new StreamReader(stream, Encoding.ASCII); | ||
using var writer = new StreamWriter(stream, Encoding.ASCII); | ||
while (true) { | ||
Console.Write("Enter some text to send: "); | ||
var input = Console.ReadLine(); | ||
await writer.WriteAsync(input); | ||
await writer.FlushAsync(); | ||
var response = await reader.ReadLineAsync(); | ||
Console.WriteLine($"Received: {response}"); | ||
} //this just loops forever and c# is smart enough not to need a 'return' -- neat | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Appetizer - reflect server | ||
|
||
This sample demonstrates how to write data to a stream and receive data. It uses the https://openziti.io/appetizer | ||
demonstration environment which is capable of genearting an identity for you, to be used with this sample. It | ||
utilizes the reflect server functionality to return whatever data you send to the server back to you. The reflect | ||
server also is tied to an LLM which is attempting to classify your input. If you use something that seems "hateful" | ||
you will receive an error message indicating your input was not accepted. The server is written in go and the | ||
source for the server is available at https://github.com/openziti-test-kitchen/appetizer. | ||
|
||
## OpenZiti Concepts Demonstrated | ||
|
||
This sample demonstrates some key OpenZiti concepts: | ||
|
||
* Application-embedded zero trust client written in C# to a golang-based application embedded zero trust server. | ||
* Sending data in a line-delimeted protocol to a server deployed out in the cloud, then receiving and displaying | ||
the response. | ||
* Directly dialing a service by service name, in this case it is named "reflectService" | ||
* Creating a a strong identity by enrolling a one-time use token provided by the appetizer server. | ||
|
||
## Setup | ||
|
||
This sample is unlike others. It relies on an instance of the appetizer demo to be deployed. It's easiest to use | ||
the instance OpenZiti has deployed by going to https://appetizer.openziti.io/. You'll see a screen asking you to | ||
enter something unique, like your email address. There's a button to click named "Add to OpenZiti". After you click | ||
the button, you'll be able to save/download a one-time use token. | ||
|
||
To run the sample, simply provide the path to that token as a argument to the program. | ||
|
||
## Running the Sample | ||
|
||
After downloading the .jwt, you should be able to just run it directly. | ||
|
||
Example: | ||
``` | ||
dotnet run --project OpenZiti.NET.Samples/OpenZiti.NET.Samples.csproj appetizer-reflect | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
Copyright NetFoundry Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
https://www.apache.org/licenses/LICENSE-2.0 | ||
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. | ||
*/ | ||
|
||
using System.IO; | ||
using System.Text; | ||
|
||
namespace OpenZiti.NET.Samples.src.Common; | ||
internal class AppetizerSetup { | ||
private static readonly NLog.Logger Log = NLog.LogManager.GetCurrentClassLogger(); | ||
internal static ZitiContext ContextFromFile(string idFile) { | ||
if (idFile.EndsWith(".json")) { | ||
// good - we'll just use it | ||
} else if (idFile.EndsWith(".jwt")) { | ||
// infer it's a jwt to be enrolled... strip .jwt and find a .json and use THAT if it's here... | ||
var idFileJson = idFile.Replace(".jwt", ".json"); | ||
if (File.Exists(idFileJson)) { | ||
// use it | ||
idFile = idFileJson; | ||
} else { | ||
// assume we need to enroll the file | ||
Log.Info($"{idFileJson} doesn't exist. Assuming this is a token to enroll..."); | ||
|
||
var strongIdentity = API.EnrollIdentityFile(idFile); | ||
File.WriteAllBytes($"{idFileJson}", Encoding.UTF8.GetBytes(strongIdentity)); | ||
Log.Info($"Strong identity written to: {idFileJson}"); | ||
idFile = idFileJson; | ||
} | ||
} | ||
|
||
var idFileBytes = File.ReadAllText(idFile); | ||
var c = new ZitiContext(idFileBytes); //demonstrates loading an identity via json, not as a file | ||
return c; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters