Now by creating our own simple application,
we're going to create a simple Python script,
we're able to create our own profile and get a better idea
of what AppArmor is going to do for us.
We can go through and create our script.
Using any editor you want, I'm using Vim,
but don't feel pressured,
we can go through and make sure that it can run independently,
so we're putting the path through to our interpreter.
We're setting up a variable where we've got the name of the file.
I'm just calling it mytextfile.
There's no path.
It's going to be in our current working directory.
We can go into a conditional statement, try,
so we can go ahead and try and open and close a FILE.
The FILE we're referencing is the variable.
If that succeeds, we'll print a message.
If it fails, we can go into our exception block,
and we will then go through and print another message
saying we Failed to create the file, and exit our script.
The first thing that we need to do is obviously dive out to
our command line and make sure we can run this script and
make sure that running unconfined,
where we're not working with our profile at the moment,
it is going to execute properly.
Come on, let's go make a script.
So here we are on our command line,
and we want to start stepping through the process of
generating our own AppArmor profiles, allowing us to protect our applications.
After all,
we don't want the risk of somebody maliciously gaining control of
an application or a script that we've created.
So what we're going to do here is we're going to
use any text editor that we want.
I'm just using Vim.
And we can go through and create then our test.py file.
Inside of our Python script,
usually the first thing we want to do is put the
path through to our interpreter.
We'll keep this very simple.
So I'm going to go through and hit the INSERT,
and as we step our way through,
we're putting in then the path through to usr/bin/python3.
With that done and dusted, we're going to create ourselves a variable.
So we're going to create a variable called FILE,
and we're going to have it set to, and we can set it through to mytext,
whatever name we want to give to the file.
Now our main code is going to begin with a try block.
So this is great if we want to test to see if something is going to work or not.
We can go try and colon.
That indicates we go into a code block.
In Python,
we need to make sure that we're indenting our code so
we know we belong to this try block.
And all we're going to do then is open,
we're going to specify the file that we want to open,
and that is going to be our variable, comma,
and then the mode.
We're going to open it up in append mode.
I'm going to close off my parentheses for my open function,
and straightaway I just want to close the file.
We just want to create a new and empty file,
so I can get through then and close this off.
Once we've succeeded, again, I'm still part of the try block,
so I'm just spacing two spaces in.
Don't use the Tab key unless you know you've set up
your tabs to be interpreted as spaces.
We should be using spaces.
With this done, we're going to print out a nice little success message.
So we're going to use print, open up our function,
and we're going to do an f print, allowing us to combine our variable together,
and then we can say Created file: and then the file that we created,
which is going to be our variable.
Variables are just added in our brace brackets.
Close off our quotes, close off our parentheses.
Now all of that will happen if we succeeded.
If we failed, we will then go through to our exception.
So we put except, and we can then go through and just print out a message.
So, again, two spaces to show that I'm indented inside of my exception,
and we can do pretty much the same thing,
print.
As we then go through, we'll do our formatted print,
and we can say Failed to create, and then put our FILE.
With that done,
let's close off our parentheses and make sure we don't
proceed with the rest of the script, even though there is nothing to follow.
We can then exit our script with an error code of 1.
Now that's it.
Python doesn't have to be complex,
but just make sure we are using spaces when we're using our indentation.
With this done, I'm going to go through and save it.
I want to execute this independently,
so I need to make sure we've got our chmod u+x for my script,
and I can check that it's going to work.
So if I go through and run then this program,
we can see then that we've created the file.
It will created in this directory, and we can see there that the file exists.
Glorious, isn't it?
Let's go through and delete mytext.
We want to make sure when we create our profile,
that we do everything that is necessary,
including creating the file if it doesn't exist.
So we make sure that we're running this on a clean system,
not with elements that have already been done, but we're ready now to go through and take a look at profiling.